C# Program to Implement Sequential Sort

This is a C# Program to implement sequential sort.

Problem Description

This C# Program Implements Sequential Sort.

Problem Solution

Here the array is sorted using a sequential algorithm and the sorted array is displayed.

Program/Source Code

Here is source code of the C# Program to Implement Sequential Sort. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Implement Sequential Sort
 */
using System;
using System.Collections.Generic;
public static class BubbleSortMethods
{       
  public static void BubbleSort<T>(this List<T> list) where T : IComparable
  {
      bool changes;
      int count = list.Count;
       do
       {
             changes = false;
             count--;
             for (int i = 0; i < count; i++)
             {
                    if (list[i].CompareTo(list[i + 1]) > 0)
                    {
                        T temp = list[i + 1];
                        list[i + 1] = list[i];
                        list[i] = temp;
                        changes = true;
                    }
                }
            } while (changes);
        }
    }
 
    class Program
    {
        static void Main()
        {
            List<int> testList = new List<int> { 3, 17, 13, 2, 11, 20, 10, 14, 4 };
            testList.BubbleSort();
            Console.WriteLine("The Sorted Array is : ");
            foreach (var t in testList) Console.Write(t + " ");
            Console.ReadLine();
        }
    }
Program Explanation

This C# program is used to implement a sequential sort. We have already defined an array of elements to testlist variable. Using ‘testlist’ variable perform the BubbleSort() procedure. The BubbleSort() procedure is used to sort the array of an element. Using do while loop decrements the value of ‘count’ variable.

advertisement
advertisement

For loop is used to sort the array elements, if condition statement is used to check that the elements present in the value of an array is greater than 0. If the condition is true then execute the iteration of the statement.

Interchange the value of ‘present’ variable to the value of ‘next’ variable using temporary variable ‘temp’. Here the array is sorted using a sequential algorithm. Print the sequential sort of an element value.

Runtime Test Cases
 
The Sorted Array is : 
2  3  4  10  11  13  14  17  20

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.