Array Applications - faculty-web.msoe.edu 1910 –Winter 2017/18 3 © tj Array Applications...
Transcript of Array Applications - faculty-web.msoe.edu 1910 –Winter 2017/18 3 © tj Array Applications...
2 © tjEE 1910 – Winter 2017/18
Array Applications
• Sorting
• Want to put the contents of an array in order
• Selection Sort
• Bubble Sort
• Insertion Sort
• Quicksort
• Quickersort
3 © tjEE 1910 – Winter 2017/18
Array Applications
• Bubble Sort - conceptual
• Sort an array of numbers into ascending or descending order• Split the list into 2 parts: sorted and unsorted
• Find the smallest(largest) element in the unsorted part of the list
• Move that element to the end of the sorted list
• Move the sort boundary up by 1 element
sortboundary
sorted unsorted
4 © tjEE 1910 – Winter 2017/18
Array Applications
• Bubble Sort - conceptual
sortboundary
sorted unsorted
sortboundary
sorted unsorted
5 © tjEE 1910 – Winter 2017/18
Array Applications
• Bubble Sort – implementation
• How do we find the smallest(largest) element in the unsorted list?• Bubble it up to the beginning of the list
• Compare 2 side by side elements• If in correct order, small large(large small), leave them
alone
• If not in correct order, swap them
-2 -1 3 1 8 9 2 3
sortboundary
sorted unsorted
6 © tjEE 1910 – Winter 2017/18
Array Applications
• Bubble Sort – implementation
-1 3 1 8 9 2 3
sorted unsorted
-1 3 1 8 9 2 3
sorted unsorted
-1 3 1 8 2 9 3
sorted unsorted
-1 3 1 2 8 9 3
sorted unsorted
-1 3 1 2 8 9 3
sorted unsorted
-1 1 3 2 8 9 3
sorted unsorted
swap
swap
swap
Shift boundary
7 © tjEE 1910 – Winter 2017/18
Array Applications
• Bubble Sort – implementationBubble Sort
bdry = 0bdry ++
bub = lastbub --
ary[bub] <ary[bub – 1]
swap
y
bdry< last
bub >bdry
y
y
n
n
n
8 © tjEE 1910 – Winter 2017/18
Array Applications
• Bubble Sort – implementationBubble Sort
bdry = 0bdry ++
bub = lastbub --
ary[bub] <ary[bub – 1]
swap
y
bdry< last
bub >bdry
y
y
n
n
n
void bubbleSort(int myArray[], int last){// local variablesint tmp;int bdry;int bub;
// outer loopfor(bdry = 0; bdry < last; bdry++){
// inner loopfor(bub = last; bub > bdry; bub--){
if(myArray[bub] < myArray[bub – 1]){tmp = myArray[bub];myArray[bub] = myArray[bub – 1];myArray[bub – 1] = tmp;
} // end if} // end inner
} // end outerreturn;
} // end bubbleSort
9 © tjEE 1910 – Winter 2017/18
Array Applications
• Bubble Sort
• Efficiency - Bubble sort takes
• N outer loops
• (N-1)! inner loops
• and a maximum of (N-1)! exchanges
10 © tjEE 1910 – Winter 2017/18
Array Applications
• Searching
• Want to determine if and where something is in an array
• Sequential Search
• Binary Search
11 © tjEE 1910 – Winter 2017/18
Array Applications
• Sequential Search
• Check each array value for the item you are looking for
• Takes a maximum of N checks
• N = 1M, upto 1M checks
12 © tjEE 1910 – Winter 2017/18
Array Applications
• Binary Search
• Requires the data to be sorted
• Reduces the number of checks to log2N+1
• N = 1M, 21 checks
mid lastfirst
13 © tjEE 1910 – Winter 2017/18
Array Applications
• Binary Search
• Find the mid point between first and last(indexes)
• Compare the target with the value at mid
• If value is greater than mid set first to mid + 1
• If value is less than mid set last to mid -1
• If value = target return the index
mid lastfirst
14 © tjEE 1910 – Winter 2017/18
Array Applications
• Binary Search – looking for 5
1 2 3 4 5 7 8 9 10 11 12 13
mid lastfirst
1 2 3 4 5 7 8 9 10 11 12 13
mid lastfirst
1 2 3 4 5 7 8 9 10 11 12 13
firstmid
last
target < mid
Set last to mid-1Reset mid
target > mid
Set first to mid+1Reset mid
target > mid
Set first to mid+1Reset mid
1 2 3 4 5 7 8 9 10 11 12 13
first, mid, last
target = midreturn mid
15 © tjEE 1910 – Winter 2017/18
Array Applications
• Binary Search – looking for 6
1 2 3 4 5 7 8 9 10 11 12 13
mid lastfirst
1 2 3 4 5 7 8 9 10 11 12 13
mid lastfirst
1 2 3 4 5 7 8 9 10 11 12 13
firstmid
last
target < mid
Set last to mid-1Reset mid
target > mid
Set first to mid+1Reset mid
target > mid
Set first to mid+1Reset mid
1 2 3 4 5 7 8 9 10 11 12 13
first, mid, last
target > midSet first to mid+1
first > lastSTOP – not found1 2 3 4 5 7 8 9 10 11 12 13
last first
16 © tjEE 1910 – Winter 2017/18
Array Applications
• Binary Search – implementation
Binary Search
first <= last
first = 0last = end
target >ary[mid]
mid =(first + last)/2
target <ary[mid]
first = mid + 1
first = mid + 1first = last + 1*locn = midfound = 1
y
yy
n
n
n
17 © tjEE 1910 – Winter 2017/18
Array Applications
• Binary Search – implementation
int binarySearch(int myArray[], int end, int target, int* locn){// Binary Search Function//// Inputs: Array to sort, index of last element,// value to search for, pointer to location// to store the index of the value if found// Outputs: Returns 1 if value found, 0 if not// Modifies the value corresponding to the pointer//// local variablesint first;int mid;int last;
// algorithmfirst = 0;last = end;
while(first <= last){// calculate midmid = (first + last)/2;
// check valueif(target > myArray[mid])
// upper halffirst = mid + 1;
else if(target < myArray[mid])// lower halflast = mid - 1;
else// foundfirst = last + 1;
} // end while
// set value of index// using a pointer to allow multiple returns*locn = mid;
// set return to 1 if found, 0 if not foundreturn (target == myArray[mid]);
}