Principles of Computer Science I Honors Section
description
Transcript of Principles of Computer Science I Honors Section
![Page 1: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/1.jpg)
1
Principles of Computer Science I
Honors Section
Note Set 5CSE 1341
![Page 2: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/2.jpg)
2
Today:
Searching and
Sorting Arrays
![Page 3: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/3.jpg)
3
Searching
• Looking for a particular value in an array
• Linear Search– Start at the beginning (or end) and
iteratively check every element• Until you find it • OR until you reach the end
• Binary Search– Start in the middle of a sorted array
• Eliminates ½ of the remaining search space on each iteration
![Page 4: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/4.jpg)
4
Linear Search
int test[5]={10, 42, 8, 100, -3};int result;result = searchArray(test, 5, 100);if (result == -1)
cout << “100 Not Found” << endl;else
cout << “100 Found” << endl;
//Returns Subscript of element or -1 if not foundint searchArray(int arr[], int numElem, int target){
for(int x = 0; x < numElem; x++){ if (arr[x] == target)
return x;}return (-1);
}
![Page 5: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/5.jpg)
5
Searching • Also Searching
– Finding the largest or smallest element in an array //Returns Subscript of largest element int
//Largest value placed in valsearchArray(int arr[], int numElem, int& val){
val = arr[0];int subs = 0;for(int x = 1; x < numElem; x++){ if (arr[x] > val)
{ val = arr[x];
subs = x;}
}return subs;
}
![Page 6: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/6.jpg)
6
Binary Search
![Page 7: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/7.jpg)
7
Binary Search• Array must be sorted
• Only searching ½ of the array in each iteration of the search
int main(){ int test[13]={1,5,7,8,10,11,14,16,22,
35,44,57,66}; int result = binarySearch(test,13,16); if(result == -1)
cout<<“Number not found”<<endl; else
cout<<“Number found”<<endl;return 0;
}
![Page 8: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/8.jpg)
8
Binary Search - Intuition
6657443522161411108751
[0] [12][6]
16 value
We ask: Is Value in the top half of the array, or the bottom half?
Determine this based on comparing value to middle element
![Page 9: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/9.jpg)
9
Binary Search - Implementation
int binarySearch(int arr[], int numElem, int val){int first = 0, last = numElem – 1, middle;while(first <= last){
middle=first+(last-first)/2;if (arr[middle]==val)
return middle;else if(arr[middle]>val)
last = middle – 1;else
first = middle + 1;}return -1;
}
find the middle index
If val is in bottom half
If val is in top half
![Page 10: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/10.jpg)
10
Binary Search - Intuition
6657443522161411108751
[0] [12][6]
16 val
middle = first+(last-first)/2 = 6arr[middle] = 14
Is val >, < or == 14
>
first last
so: first = middle + 1first = 7
Iter 1
![Page 11: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/11.jpg)
11
Binary Search - Intuition
6657443522161411108751
[0] [12][6]
16 val
middle = first+(last-first)/2 = 9arr[middle] = 35
Is val >, < or == 35
firstlast
Iter 2
first = 7last = 12
[9][7]
< so: last = middle - 1last = 8
![Page 12: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/12.jpg)
12
Binary Search - Intuition
6657443522161411108751
[0] [12][6]
16 val
middle = first+(last-first)/2 = 7arr[middle] = 16
Is val >, < or == 16
first last
Iter 3
first = 7last = 8
[9][7]
= so: return middlereturn 7
[8]
![Page 13: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/13.jpg)
13
Sorting
![Page 14: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/14.jpg)
14
Motivation
• Searching sorted data is faster than searching unsorted data– What if the phonebook wasn’t in
alphabetical order?
• Sorted data is usually easier to deal with
• Can sort ascending or descending
![Page 15: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/15.jpg)
15
Bubble Sort Intuition
2135
swap if out of order
213 5
swap if out of order
213 5
213 5
21 3 5
21 3 5
…and so on untilyou make one passwithout swapping
![Page 16: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/16.jpg)
16
Bubble Sort – Swap Function
void swap(int& x, int& y)
{
int temp = x;
x = y;
y = temp;
}
![Page 17: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/17.jpg)
17
Bubble Sortvoid bubbleSort(int arr[], int numElem){bool swapped;do{
swapped = false;for (int x = 0; x < (numElem-1); x++){
if (arr[x] > arr[x+1]){
swap(arr[x], arr[x+1]);swapped = true;
}}
} while (swapped);}
![Page 18: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/18.jpg)
18
Bubble Sort - Example
198327
swapped = false
arr
[0] [1] [5]
if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true;}
true
72
x=0
72
![Page 19: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/19.jpg)
19
Bubble Sort - Example
19832 7
swapped = true
arr
[2][1] [5]
if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true;}
73
x=1
![Page 20: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/20.jpg)
20
false – do nothing
Bubble Sort - Example
19832 7
swapped = true
arr
[2] [3] [5]
if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true;}
x=2
![Page 21: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/21.jpg)
21
false – do nothing
Bubble Sort - Example
19832 7
swapped = true
arr
[4][3] [5]
if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true;}
x=3
![Page 22: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/22.jpg)
22
Bubble Sort - Example
19832 7
swapped = true
arr
[4] [5]
if(arr[x]>arr[x+1]){ swap(arr[x], arr[x+1]); swapped = true;}
x=4
1 9
Finished w/ 1st Iteration of do-while loop. . .
![Page 23: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/23.jpg)
23
Bubble Sort - Example
1 9832 7
swapped = false
arr
[1] [5][0] [2] [3] [4]
1 8
true
Finished w/ 2nd Iteration of do-while loop. . .
![Page 24: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/24.jpg)
24
Bubble Sort - Example
1 9832 7
swapped = false
arr
[1] [5][0] [2] [3] [4]
true
Finished w/ 3rd Iteration of do-while loop. . .
1 7
![Page 25: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/25.jpg)
25
Bubble Sort - Example
7 9832 1
swapped = false
arr
[1] [5][0] [2] [3] [4]
true
Finished w/ 4th Iteration of do-while loop. . .
31
![Page 26: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/26.jpg)
26
Bubble Sort - Example
7 9812 3
swapped = false
arr
[1] [5][0] [2] [3] [4]
true
Finished w/ 5th Iteration of do-while loop. . .but still must go through one more time!
1 2
![Page 27: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/27.jpg)
27
Bubble Sort - Example
7 9821 3
swapped = false
arr
[1] [5][0] [2] [3] [4]
DONE!
![Page 28: Principles of Computer Science I Honors Section](https://reader035.fdocuments.in/reader035/viewer/2022062305/5681482b550346895db54f3f/html5/thumbnails/28.jpg)
28
Questions???
?