system programming in c lecture 4. lecture summary operations with arrays: –searching the array...

Post on 18-Dec-2015

227 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

Sequential Search Algorithm

1 4 7 5 2 3 8 6

41

1 4 7 5 2 3 8 6

42

3 Found!

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

}

Sequential Search Performance

• Worst-case time: O(N)

• Best-case time: O(1)

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

Binary Search Algorithm

1 2 3 4 5 6 7

61

1 2 3 4 5 6 7

62

3 Found!

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

}

Binary Search Performance

Sorting

• One of the most important operations performed by computers

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

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.

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

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

}

}

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?

Performance

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

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

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.

Insert sort

7 2 3 4 5 1 86

1

2

7 3 4 5 1 86

2

2 3

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

}}

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)

Merge Sort

• One of the earliest forms of computer sorting.

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

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

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

}}

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

}

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

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

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

Quicksort

Sort Left Partition in the same way

Initial Step - First Partition

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

}}

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

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;

}

Performance

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

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

top related