# Insertion Sort actually applied for sorting small arrays of data

 Дата канвертавання 26.04.2016 Памер 33.39 Kb.
 Insertion Sort Insertion sort actually applied for sorting small arrays of data. Insertion sort algorithm somewhat resembles selection sort. Array is imaginary divided into two parts - sorted one and unsorted one. At the beginning, sorted part contains first element of the array and unsorted one contains the rest. At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. When unsorted part becomes empty, algorithm stops. Sketchy, insertion sort algorithm step looks like this: becomes Let us see an example of insertion sort routine to make the idea of algorithm clearer. E xample. Sort {7, -5, 2, 16, 4} using insertion sort. The ideas of insertion The main operation of the algorithm is insertion. The task is to insert a value into the sorted part of the array. Let us see the variants of how we can do it. "Sifting down" using swaps The simplest way to insert next element into the sorted part is to sift it down, until it occupies correct position. Initially the element stays right after the sorted part. At each step algorithm compares the element with one before it and, if they stay in reversed order, swap them. Let us see an illustration. This approach writes sifted element to temporary position many times. Next implementation eliminates those unnecessary writes. Shifting instead of swapping We can modify previous algorithm, so it will write sifted element only to the final correct position. Let us see an illustration. It is the most commonly used modification of the insertion sort. Insertion sort properties adaptive (performance adapts to the initial order of elements); stable (insertion sort retains relative order of the same elements); in-place (requires constant amount of additional space); online (new elements can be added during the sort). Insertion Sort C++ Code : Insertion_Sort( List, N) {             for (i = 1; i < N; i++) {             j = i;             while (j > 0 && List[j - 1] > List[j]) { swap(Lis t[j], List[j-1])                   j--;             }       } } Selection Sort The idea of algorithm is quite simple. Array is imaginary divided into two parts - sorted one and unsorted one. At the beginning, sorted part is empty, while unsorted one contains whole array. At every step, algorithm finds minimal element in the unsorted part and adds it to the end of the sorted one. When unsorted part becomes empty, algorithm stops. When algorithm sorts an array, it swaps first element of unsorted part with minimal element and then it is included to the sorted part. This implementation of selection sort in not stable. In case of linked list is sorted, and, instead of swaps, minimal element is linked to the unsorted part, selection sort is stable. Let us see an example of sorting an array to make the idea of selection sort clearer. Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Selection Sort C++ Code : SelectionSort(int arr[ ], int n) {       for (i = 0; i < n - 1; i++) {             minIndex = i;             for (j = i + 1; j < n; j++)                   if (arr[j] < arr[minIndex])             minIndex = j;             if (minIndex != i) swap( arr[i], arr[minIndex]);       } } Bubble Sort Bubble sort is a simple and well-known sorting algorithm. It is used in practice once in a blue moon and its main application is to make an introduction to the sorting algorithms. Bubble sort belongs to O(n2) sorting algorithms, which makes it quite inefficient for sorting large data volumes. Bubble sort is stable and adaptive. Algorithm Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them. If at least one swap has been done, repeat step 1. You can imagine that on every step big bubbles float to the surface and stay there. At the step, when no bubble moves, sorting stops. Let us see an example of sorting an array to make the idea of bubble sort clearer. Example. Sort {5, 1, 12, -5, 16} using bubble sort. Turtle example. Thought, array {2, 3, 4, 5, 1} is almost sorted, it takes O(n2) iterations to sort an array. Element {1} is a turtle. Rabbit example. Array {6, 1, 2, 3, 4, 5} is almost sorted too, but it takes O(n) iterations to sort it. Element {6} is a rabbit. This example demonstrates adaptive property of the bubble sort. void bubbleSort(int arr[], int n) {       bool swapped = true;       int j = 0;       int tmp;       while (swapped) {             swapped = false;             j++;             for (int i = 0; i < n - j; i++) {                   if (arr[i] > arr[i + 1]) {                         tmp = arr[i];                         arr[i] = arr[i + 1];                         arr[i + 1] = tmp;                         swapped = true;                   }             }       } } Quicksort Quicksort is a fast sorting algorithm, which is used not only for educational purposes, but widely applied in practice. On the average, it has O(n log n) complexity, making quicksort suitable for sorting big data volumes. The idea of the algorithm is quite simple and once you realize it, you can write quicksort as fast as bubble sort. Algorithm The divide-and-conquer strategy is used in quicksort. Below the recursion step is described: Choose a pivot value. We take the value of the middle element as pivot value, but it can be any value, which is in range of sorted values, even if it doesn't present in the array. Partition. Rearrange elements in such a way, that all elements which are lesser than the pivot go to the left part of the array and all elements greater than the pivot, go to the right part of the array. Values equal to the pivot can stay in any part of the array. Notice, that array may be divided in non-equal parts. Sort both parts. Apply quicksort algorithm recursively to the left and the right parts. Partition algorithm in detail There are two indices i and j and at the very beginning of the partition algorithm i points to the first element in the array and j points to the last one. Then algorithm moves i forward, until an element with value greater or equal to the pivot is found. Index j is moved backward, until an element with value lesser or equal to the pivot is found. If i ≤ j then they are swapped and i steps to the next position (i + 1), j steps to the previous one (j - 1). Algorithm stops, when i becomes greater than j. After partition, all values before i-th element are less or equal than the pivot and all values after j-th element are greater or equal to the pivot. Example. Sort {1, 12, 5, 26, 7, 14, 3, 7, 2} using quicksort. Notice, that we show here only the first recursion step, in order not to make example too long. But, in fact, {1, 2, 5, 7, 3} and {14, 7, 26, 12} are sorted then recursively. Why does it work? On the partition step algorithm divides the array into two parts and every element a from the left part is less or equal than every element b from the right part. Also a and b satisfy a ≤ pivot ≤ b inequality. After completion of the recursion calls both of the parts become sorted and, taking into account arguments stated above, the whole array is sorted. void quickSort(int arr[], int left, int right) {       int i = left, j = right;       int tmp;       int pivot = arr[(left + right) / 2];         /* partition */       while (i <= j) {             while (arr[i] < pivot)                   i++;             while (arr[j] > pivot)                   j--;             if (i <= j) {                   swap(arr[i], arr[j]);                   i++;  j--;             }       };         /* recursion */       if (left < j)             quickSort(arr, left, j);       if (i < right)             quickSort(arr, i, right); }

База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі