Selection sort is a simple algorithm, but inefficient.
First, find the smallest element using a linear scan and move it to the front (swap). Then, find the second smallest and move it, again doing a linear scan. Continue doing this until all the elements are in place.
voidSwap(int[] array,int index1,int index2){int temp =array[index1];array[index1] =array[index2];array[index2] = temp;}voidSelectionSort(int[] array){var n =array.Length-1;for(int i =0; i <= n; i++){var minIdx = i;for(int j = i +1; j <= n; j++){if(array[j] <array[minIdx]){ minIdx = j; } }Swap(array, minIdx, i); }}
Time Complexity: O(n^2)
Space Complexity: O(1)
Bubble Sort
In this algorithm, we start at the beginning of the array and swap the first two elements if the first is greater than the second. Then, we go to the next pair, and so on, continuously making sweeps of the array until it is sorted.
voidBubbleSort(int[] array){var n =array.Length;for(int i =0; i < n -1; i++){for(int j =0; j < n - i -1; j++){if(array[j] >array[j +1]){int temp =array[j];array[j] =array[j +1];array[j +1] = temp; } } } }
Time Complexity: O(n^2)
Space Complexity: O(1)
Insertion Sort
voidInsertionSort(int[] array){var n =array.Length-1;for(int i =1; i <= n; i++){var unsortedItem =array[i];var comparingItemIndex = i -1;while(comparingItemIndex >=0&& unsortedItem <array[comparingItemIndex]){array[comparingItemIndex +1] =array[comparingItemIndex]; comparingItemIndex--; }array[comparingItemIndex +1] = unsortedItem; }}
Time Complexity: O(n^2)
Space Complexity: ?
Quick Sort
Quicksort is another sorting algorithm based on the Divide and Conquer programming technique. In this algorithm, an element is first chosen as the pivot, and the entire array is then partitioned around this pivot.
As you've probably guessed, a good pivot is crucial for an efficient sort. The pivot can either be a random element, the media element, the first element, or even the last element.
Implementations of quicksort often differ in the way they choose a pivot. In the average case, quicksort will sort a large array with a good pivot in just O(nlogn) time.
The general pseudocode of quicksort repeatedly partitions the array on the pivot and positions it in the correct position of the subarray. It also places the elements smaller than the pivot to its left and elements greater than the pivot to its right.