public static int[] InsertionSorting(int[] inputData) { for (int i = 0; i < inputData.Length; i++) { int j = i; int keydata = inputData[i]; while (j > 0 && inputData[j - 1] > keydata) { inputData[j] = inputData[j - 1]; j = j - 1; } inputData[j] = keydata; } Console.WriteLine("Sorted data is :- " + SortingLogic.ArrayToString(inputData)); return(inputData); }
static void Main(string[] args) { int[] inputArray = { 10, 5, 15, 18, 19, 42, 15, 32, 99, 100 }; Console.WriteLine("input data is :- " + SortingLogic.ArrayToString(inputArray)); SortingLogic.InsertionSorting(inputArray); Console.ReadLine(); Console.Write("by quick sort"); int[] array = { 10, 5, 15, 18, 19, 42, 15, 32, 99, 100, -15, -1, 500 }; Console.WriteLine("input data is :- " + SortingLogic.ArrayToString(array)); QuickSort q_Sort = new QuickSort(array); q_Sort.quickSort(); Console.WriteLine("Sorted data is :- " + SortingLogic.ArrayToString(array)); Console.ReadLine(); }
public static int[] BubbleSorting(int[] inputData) { for (int i = 0; i < inputData.Length; i++) { for (int j = 1; j < inputData.Length; j++) { if (inputData[j - 1] > inputData[j]) { //swap the data int temp = inputData[j]; inputData[j] = inputData[j - 1]; inputData[j - 1] = temp; } } } Console.WriteLine("Sorted data is :- " + SortingLogic.ArrayToString(inputData)); return(inputData); }
public static int[] QuickSorting(int[] inputData) { int indexPosition = 0; for (int i = 0; i < inputData.Length; i++) { int pivot = inputData[inputData.Length - 1]; for (int j = 1; i < inputData.Length; i++) { if (inputData[j] < pivot) { int temp = inputData[j]; inputData[j] = inputData[indexPosition]; inputData[indexPosition] = temp; indexPosition = indexPosition + 1; } } } Console.WriteLine("Sorted data is :- " + SortingLogic.ArrayToString(inputData)); return(inputData); }