33
System Programming in C Lecture 4

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

Embed Size (px)

Citation preview

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

System Programming in C

Lecture 4

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

Lecture Summary

• Operations with Arrays:– Searching the array– Sorting the array

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

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

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

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

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

Sequential Search Algorithm

1 4 7 5 2 3 8 6

41

1 4 7 5 2 3 8 6

42

3 Found!

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

Sequential Search Algorithm

int seq_search( int array[], int lower, int upper, int value)

{

int index;

for ( index = lower; index <= upper; index++ )

if (array[index] == value)

return index;

return -1; /* not found*/

}

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

Sequential Search Performance

• Worst-case time: O(N)

• Best-case time: O(1)

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

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

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

Binary Search Algorithm

1 2 3 4 5 6 7

61

1 2 3 4 5 6 7

62

3 Found!

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

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);

}

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

Binary Search Performance

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

Sorting

• One of the most important operations performed by computers

• Will discuss 4 types of sorting algorithms: select, insert, merge, split sort.

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

Select sort

• Define selecting as finding i-th maximum value.• Algorithm is – find largest value, place it on the

top (N), find 2nd 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.

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

Select Sort – Algorithm

1 5 3 2 5 7 861

1 5 3 2 5 6 872

max

1 5 3 2 8 7 56

max

0

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

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]);

}

}

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

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?

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

Performance

• Bubble sort – O(N2) compare and swap operations

• General select sort - O(N2) compare operations, O(N) swap operations.

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

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.

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

Insert sort

7 2 3 4 5 1 86

1

2

7 3 4 5 1 86

2

2 3

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

Insert sort

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 */

}}

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

Insert sort performance

• Best case: O(N)

• Worst case: O(N2)

• By replacing linear search with binary search, may make worst-case performance O(NlogN)

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

Merge Sort

• One of the earliest forms of computer sorting.

• Algorithm:– Split list into two– Sort each of them– Merge the lists

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

Merge Sort

7 4 31 Initial Array 2

72 4 3

72 3 4

3 4 724 Merge Sorted arrays

3 Sort sub-arrays

2 Split into 2

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

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*/

}}

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

Merge – slightly more complex

1. Move common part of arraysint 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 2nd array*/low2++;

}else {save[next] = array[low1]; /* …or from 1st */low1++;

}next++;

}

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

Merge contd.

2. Move the rest of the longest arrayif (mid >= low1) /* 1st array is the longest*/

for (index = 0; index <= mid - low1; index ++)

save[next + index] = array[low1 + index];

else /* 2nd array is the longest*/

for (index = 0; index <= high - low2; index ++)

save[next + index] = array[low2 + index];

3. Copy back to original arrayfor (index = low; index <= high; index++)

array[index] = save[index-low];

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

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

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

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

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

Quicksort

Sort Left Partition in the same way

Initial Step - First Partition

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

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 );

}}

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

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

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

Quicksort – partition phasepartition( 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;

}

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

Performance

• Best-case scenario: O(N*log N)

• Worst-case scenario: O(N^2)