Presentation is loading. Please wait.

Presentation is loading. Please wait.

System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array.

Similar presentations


Presentation on theme: "System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array."— Presentation transcript:

1 System Programming in C Lecture 4

2 Lecture Summary Operations with Arrays: –Searching the array –Sorting the array

3 Array Searching methods Computer systems are often used to store large amounts of data from which individual records must be retrieved according to some search criterion Will discuss two types of searches – Sequential and binary searches

4 Sequential Searches Just looking through the collection of elements To Compare the performance of the methods may compare: –Average time (typically quite complex to compute) –Worst-case time – guaranteed performance –Best-case time

5 Sequential Search Algorithm 14752386 4 1 14752386 4 2 3Found!

6 Sequential Search Algorithm int seq_search( int array[], int lower, int upper, int value) { intindex; for ( index = lower; index <= upper; index++ ) if (array[index] == value) return index; return -1;/* not found*/ }

7 Sequential Search Performance Worst-case time: O(N) Best-case time: O(1)

8 Binary Search Algorithm is: –Compare with the middle of array –If equal – immediately return –If key is less then middle, item is in lower half of the array –If key is greater, item is in upper half of array –Repeat the procedure with the upper (lower) half of the array

9 Binary Search Algorithm 1234567 6 1 1234567 6 2 3Found!

10 Binary Search int bin_search( int array[], int low, int high, int value ) { int mid; if (low > high) return NOT_FOUND; /* Termination check */ mid = (high+low)/2; if (array[mid] == value)/* Match, return item found */ return mid; else if (array[mid] > value) /* search lower half */ return bin_search( array, low, mid-1, value); else if (array[mid] < value) /* search upper half */ return bin_search( array, mid+1, high, value); }

11 Binary Search Performance

12 Sorting One of the most important operations performed by computers Will discuss 4 types of sorting algorithms: select, insert, merge, split sort.

13 Select sort Define selecting as finding i-th maximum value. Algorithm is – find largest value, place it on the top (N), find 2 nd largest value, place it on the top of not sorted elements (N-1), etc. At each moment – two sub-arrays – one-sorted, one-not sorted. Arrays are ranked. Repeat the process until not sorted array size reaches 1.

14 Select Sort – Algorithm 15325786 1 15325687 2 max 15328756 0

15 Select Sort-Implementation select_sort(int array[], int low, int high) { int upper_bound, index, next_max; for (upper_bound = high; upper_bound > low; upper_bound--) { next_max = upper_bound; for (index = low; index < upper_bound; index++) if (array[index] > array[next_max]) next_max = index; SWAP(int, array[next_max], array[index]); }

16 Popular Variation: Bubble sort bubble_sort(int array[], int low, int high) { int upper_bound, index; for (upper_bound = high; upper_bound > low; upper_bound--) { for (index = low; index < upper_bound; index++) if (array[index] > array[index+1]) SWAP(int, array[index], array[index+1]); } BTW, how to make Bubble sort quicker on already sorted arrays?

17 Performance Bubble sort – O(N 2 ) compare and swap operations General select sort - O(N 2 ) compare operations, O(N) swap operations.

18 Insertion Sort Principle of operation – moving from the beginning of the array, inserting next element to the appropriate place in the first half of array. Used by bridge players to sort cards.

19 Insert sort 72345186 1 2 7345186 2 23

20 insert_sort(int array[], int low, int high) { int sorted, index, temp; for (sorted = low + 1; sorted <= high; sorted++) { temp = array[sorted];/* save the value */ index = sorted - 1;/* upper bound of unsorted */ while ((index >= low) && (array[index] > temp)) { array[index+1] = array[index]; /* shift unsorted*/ index--; } array[index+1] = temp;/* insert the element */ }

21 Insert sort performance Best case: O(N) Worst case: O(N 2 ) By replacing linear search with binary search, may make worst-case performance O(NlogN)

22 Merge Sort One of the earliest forms of computer sorting. Algorithm: –Split list into two –Sort each of them –Merge the lists

23 Merge Sort 743 1 Initial Array 2 7243 7234 3472 4 Merge Sorted arrays 3 Sort sub-arrays 2 Split into 2

24 Merge Sort Implementation merge_sort(int array[], int low, int high) { int mid; if (high > low) { mid = (high + low) / 2; merge_sort(array, low, mid ); /*lower half*/ merge_sort(array, mid+1, high ); /* upper half */ merge(array, low, mid, high ); /* merge*/ }

25 Merge – slightly more complex 1. Move common part of arrays int next,low1,low2,index; int save[N]; next = low1 = low; low2 = mid +1; next = 0; while ((mid >= low1) && (high >=low2) ) { if (array[low1] > array[low2]) {/* move element …*/ save[next] = array[low2]; /* …either from 2 nd array*/ low2++; }else { save[next] = array[low1]; /* …or from 1 st */ low1++; } next++; }

26 Merge contd. 2. Move the rest of the longest array if (mid >= low1)/* 1 st array is the longest*/ for (index = 0; index <= mid - low1; index ++) save[next + index] = array[low1 + index]; else/* 2 nd array is the longest*/ for (index = 0; index <= high - low2; index ++) save[next + index] = array[low2 + index]; 3. Copy back to original array for (index = low; index <= high; index++) array[index] = save[index-low];

27 Split Sort Quicksort – most known algorithm from the group –is a very efficient sorting algorithm –invented by C.A.R. Hoare Consists of two phases: –Partition phase –Sort phase

28 QuickSort Algorithm: –Select pivot value –Split array into 2 partitions with pivot value between them –After that pivot value is on correct place in the array –Then sort partitions in the same way

29 Quicksort Sort Left Partition in the same way Initial Step - First Partition

30 Quicksort – Split phase quick_sort(int array[], int low, int high) { int pivot; /* Termination condition! */ if ( high > low ){ pivot = partition( array, low, high ); quick_sort( array, low, pivot-1 ); quick_sort( array, pivot+1, high ); }

31 Quicksort – Partition phase Goals: –Select pivot value –Move everything less pivot value to the left of it –Move everything greater than pivot value to the right of it

32 Quicksort – partition phase partition( int array[], int low, int high) { int left, right, pivot, pivot_item; pivot_item = array[low]; pivot = left = low;/* choose pivot value*/ right = high; while ( left < right ) { while( array[left] <= pivot_item ) left++; /* Move left */ while( array[right] > pivot_item ) right--; /* Move right */ if ( left < right ) SWAP(int, array[left],array[right]); } array[low] = array[right]; /* right is final position for the pivot */ array[right] = pivot_item; return right; }

33 Performance Best-case scenario: O(N*log N) Worst-case scenario: O(N^2)


Download ppt "System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array."

Similar presentations


Ads by Google