420 likes | 1.55k Views
Selection Sort. Algorithm of selection sort Get a sequence of numbers stored in an array list[n] For index variable fill from 0 to n-2 Find the index j of the smallest element in the unsorted subarray list[ fill ] through list[n-1]
E N D
Selection Sort • Algorithm of selection sort • Get a sequence of numbers stored in an array list[n] • For index variable fill from 0 to n-2 • Find the index j of the smallest element in the unsorted subarray list[fill ] through list[n-1] • If list[fill] is not the smallest, exchange list[fill] and list[j] • Output list[]
Implementation of Selection Sort with Timing void selection_sort(int list[], int n) { int fill, temp, index_of_min; for (fill = 0; fill < n; ++fill) { index_of_min = find_min(list, fill, n-1); if (fill != index_of_min) { temp = list[index_of_min]; list[index_of_min] = list[fill]; list[fill] = temp; } } } int find_min(int list[], int fill, int n){ int i, cur_min_posi; cur_min_posi = fill; for (i = fill+1; i <= n; ++i) if (list[i] < list[cur_min_posi]) cur_min_posi = i; return (cur_min_posi); } #include <stdio.h> #include <stdlib.h> /* to use rand() function */ #include <time.h> /* to use clock() function */ #define MAX 10000 void selection_sort(int *, int); int find_min(int *, int, int); int main() { int i, j, a[MAX]; clock_t ticks1, ticks2; for (j=0; j<MAX; ++j) { a[j] = rand()%100; printf("%d, ", a[j]); } ticks1=clock(); selection_sort(a, MAX); ticks2=clock(); printf("It takes %f seconds to sort and display the array.\n", (double) (ticks2-ticks1)/CLOCKS_PER_SEC); }
Time Analysis of Selection Sort void selection_sort(int list[], int n) { int fill, temp, index_of_min; for (fill = 0; fill < n; ++fill) { index_of_min = find_min(list, fill, n-1); if (fill != index_of_min) { temp = list[index_of_min]; list[index_of_min] = list[fill]; list[fill] = temp; } } } int find_min(int list[], int fill, int n){ int i, cur_min_posi; cur_min_posi = fill; for (i = fill+1; i <= n; ++i) if (list[i] < list[cur_min_posi]) cur_min_posi = i; return (cur_min_posi); } • Time analysis of selection sort. Let n be the length of the array • The number of comparisons in the condition in the for loop is n • The number of additions and assignments for ++i is 2n • The number of comparisons in find_min() is (n-1) + (n-2) + … + 1 = n(n-1) • The number of updating the cur_min_posi in find_min() is (n-1) + (n-2) + … + 1 = n(n-1) • The number of assignments for exchanges is at most 3(n-1) Total number of basic operations by CPU is at most: n + 2n + 2n(n-1) + 3(n-1) ~ 2n2
Time Analysis for Bubble Sort void bubble_sort(int a[], int n) { int i, j, t; for (j = 0; j< n; j++) for (i = 0; i < n - 1 -j; i++) if (a[i] > a[i+1]) { t = a[i]; a[i] = a[i+1]; a[i+1] = t; } } • Time analysis of bubble sort. Let n be the length of the array • The number of comparisons, assignments and increments of the outer for loop statement is about 3n • The number of comparisons, assignments and increments of the inner for loop statements is about 3(n-1)+3(n-2)+ … 3 = 3n(n-1) • Number of comparisons of the if statement is (n-1) + (n-2) + … + 1 = n(n-1) • The number of assignments for exchanges is at most3(n-1) + 3(n-2) + … + 3 = 3n(n-1) Total number of basic operations by CPU is at most 3n + 3n(n-1) + n(n-1) + 3n(n-1) ~ 7n2 • Selection sort is about three times faster than that of bubble sort in worst case (see testing example)
Function That Searches for a Target Value in an Array • Time analysis for the search algorithm Let n be the length of the array • The number of operations in the while loop statement is about 3n • The number of comparisons, and assignments of the if statement in the worst case is about 2n Total number of basic operations by CPU is about 5n Check the testing example int search(const int arr[], int target, int n) { int i, found = 0, where; i = 0; while (!found && i < n) { if (arr[i] == target) found = 1; else ++i; } if (found) where = i; else where = NOT_FOUND; return (where); }
Binary Search • Binary search problem Input: Given a sorted array, and a target. Outupt: If found, output the position of the target; otherwise -1. Binary Search Algorithm • int binary_search(const int arr[], int target, int left, int right) • { • int mid; • while (left <= right) { • mid = (right + left)/2; • if (arr[mid] == target) • return mid; • else if (target < arr[mid]) • right = mid - 1; • else left = mid + 1; • } • return NOT_FOUND; • }
Binary Search Function int binary_search(int arr[], int target, int left, int right){ int mid; while (left<=right){ mid = (right+left)/2; if (arr[mid] == target) return mid; else if (arr[mid] > target) right = mid - 1; else left = mid + 1; } return (-1); }