public static void SearchingTechniquesMain() { /* _____________________________Linear Search____________________________________ */ linear: InitialArray = ArrayHandler.GetPopulatedArray(n, minVal, maxVal); Console.WriteLine("Array: \n"); ArrayHandler.PrintArray(InitialArray); Console.WriteLine(); Console.WriteLine("Linear Search:"); Console.Write("Enter the element to be found: "); if (!int.TryParse(Console.ReadLine(), out int searchElement)) { Console.WriteLine("\nInvalid input. Please try again."); Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); Console.Clear(); goto linear; } LinearSearch(searchElement); Console.WriteLine("\n"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.Clear(); /* _____________________________Binary Search____________________________________ */ binary: InitialArray = ArrayHandler.GetPopulatedArray(n, minVal, maxVal); Console.WriteLine("Array: \n"); ArrayHandler.PrintArray(InitialArray); Console.WriteLine(); Console.WriteLine("Binary Search:"); Console.Write("Enter the element to be found: "); if (!int.TryParse(Console.ReadLine(), out searchElement)) { Console.WriteLine("\nInvalid input. Please try again."); Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); Console.Clear(); goto binary; } BinarySearch(SortingTechniques.SortForExternal((int[])InitialArray.Clone()), 0, n - 1, searchElement); Console.WriteLine(); Console.WriteLine("Press any key to continue..."); }
/* _____________________________Merge Sort____________________________________ */ // Divide and Conquer (1) private static void MergeSort(int low, int high) { if (low < high) { int mid = (low + high) / 2; //int mid = low + (high - low) / 2; MergeSort(low, mid); MergeSort(mid + 1, high); Merge(low, mid, high); ArrayHandler.PrintArray(InitialArray); } }
/* _____________________________Selection Sort____________________________________ */ // Smallest goes at the first position private static void SelectionSort() { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (InitialArray[i] > InitialArray[j]) { ArrayHandler.SwapArrayElements(InitialArray, i, j); } } ArrayHandler.PrintArray(InitialArray); } }
/* _____________________________Bubble Sort____________________________________ */ // Largest goes at the last position private static void BubbleSort() { for (int i = n - 1; i > 0; i--) { for (int j = i - 1; j >= 0; j--) { if (InitialArray[i] < InitialArray[j]) { ArrayHandler.SwapArrayElements(InitialArray, i, j); } } ArrayHandler.PrintArray(InitialArray); } }
/* _____________________________Quick Sort____________________________________ */ // Divide and Conquer (2) private static void QuickSort(int low, int high, bool toBePrinted) { if (low < high) { // pi is partitioning index, InitialArray[p] is now at right place int pi = Partition(low, high); // Separately sort elements before partition and after partition QuickSort(low, pi - 1, toBePrinted); QuickSort(pi + 1, high, toBePrinted); if (toBePrinted) { ArrayHandler.PrintArray(InitialArray); } } }
private static int Partition(int low, int high) { int pivot = InitialArray[high]; // pivot int i = low - 1; // Index of smaller element for (int j = low; j <= high - 1; j++) { // If current element is smaller than the pivot if (InitialArray[j] < pivot) { i++; // increment index of smaller element ArrayHandler.SwapArrayElements(InitialArray, i, j); } } ArrayHandler.SwapArrayElements(InitialArray, i + 1, high); return(i + 1); }
/* _____________________________Insertion Sort____________________________________ */ // Element goes at its proper position private static void InsertionSort() { for (int i = 1; i < n; i++) { int key = InitialArray[i]; int j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && InitialArray[j] > key) { InitialArray[j + 1] = InitialArray[j]; j = j - 1; } InitialArray[j + 1] = key; ArrayHandler.PrintArray(InitialArray); } }
public static void SortingTechniquesMain() { Stopwatch sw = new Stopwatch(); /* _____________________________Bubble Sort____________________________________ */ InitialArray = ArrayHandler.GetPopulatedArray(n, minVal, maxVal); Console.WriteLine("Initial Array: \n"); ArrayHandler.PrintArray(InitialArray); Console.WriteLine(); Console.WriteLine("Bubble Sort: \n"); sw.Start(); BubbleSort(); Console.WriteLine("\nTime taken: " + sw.Elapsed); sw.Reset(); Console.WriteLine("\n"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.Clear(); /* _____________________________Selection Sort____________________________________ */ InitialArray = ArrayHandler.GetPopulatedArray(n, minVal, maxVal); Console.WriteLine("Initial Array: \n"); ArrayHandler.PrintArray(InitialArray); Console.WriteLine(); Console.WriteLine("Selection Sort: \n"); sw.Start(); SelectionSort(); Console.WriteLine("\nTime taken: " + sw.Elapsed); sw.Reset(); Console.WriteLine("\n"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.Clear(); /* _____________________________Insertion Sort____________________________________ */ InitialArray = ArrayHandler.GetPopulatedArray(n, minVal, maxVal); Console.WriteLine("Initial Array: \n"); ArrayHandler.PrintArray(InitialArray); Console.WriteLine(); Console.WriteLine("Insertion Sort: \n"); sw.Start(); InsertionSort(); Console.WriteLine("\nTime taken: " + sw.Elapsed); sw.Reset(); Console.WriteLine("\n"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.Clear(); /* _____________________________Merge Sort____________________________________ */ InitialArray = ArrayHandler.GetPopulatedArray(n, minVal, maxVal); Console.WriteLine("Initial Array: \n"); ArrayHandler.PrintArray(InitialArray); Console.WriteLine(); Console.WriteLine("Merge Sort: \n"); sw.Start(); MergeSort(0, n - 1); Console.WriteLine("\nTime taken: " + sw.Elapsed); sw.Reset(); Console.WriteLine("\n"); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.Clear(); /* _____________________________Quick Sort____________________________________ */ InitialArray = ArrayHandler.GetPopulatedArray(n, minVal, maxVal); Console.WriteLine("Initial Array: \n"); ArrayHandler.PrintArray(InitialArray); Console.WriteLine(); Console.WriteLine("Quick Sort: \n"); sw.Start(); QuickSort(0, n - 1, true); Console.WriteLine("\nTime taken: " + sw.Elapsed); sw.Reset(); Console.WriteLine("\n"); Console.WriteLine("Press any key to continue..."); }