C# Program to Implement Sequential Sort
Posted by Superadmin on August 15 2022 09:41:01

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.

 

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