Download presentation
Presentation is loading. Please wait.
Published bySusan Weaver Modified over 9 years ago
1
STARTING OUT WITH STARTING OUT WITH Class 3 Honors
2
2 Objectives Understand how to search and sort a one-dimensional array
3
3 Searching Arrays Write a program that searches an array for a certain number in an array and returns the subscript where the number was found or a -1 if not found PG 482 ~ Pgrm. 8-1
4
4 Searching Arrays main ( ) { int Tests [5] = {87,75,98,100,82); int Results; Results = SearchList (Tests,5,100); if (Results == -1) cout << “No 100 found”; else { cout << “100 found”; } PG 82 ~ Pgrm. 8-1
5
5 Searching Arrays int SearchList (int List[ ],int NumElems,int Value) { for(int Count = 0;Count < NumElems;Count++) { if(List [Count] == Value) return (Count);} return (-1); } Results = SearchList (Tests,5,100); PG 482 ~ Pgrm. 8-1
6
6 Searching Arrays main ( ) { int array [20]; int small; cout <<“Enter 20 integers :”; for(int i = 0; i < 20; i++) cin >> array[i]; small = search (array); } Write a program that allows the user to enter 20 integers. The program should include a function that returns the smallest number entered
7
7 Searching Arrays int search (int elements[ ]) { int smallest=elements[0]; for(int i = 1; i < 20; i++) if(elements [i] < smallest) smallest=elements[I]; return smallest; }
8
8 Binary Search of a Sorted Array int BinarySearch(int [ ],int,int); const int ArrSize = 13; int main() { int Tests[ArrSize]={1,5,7,8,10,11,14,16, 22,35,44,57,66}; int Results, EmpId; cout<<“Enter the Employee Id you want to search for”; cin >> EmpId; // assume user enters a 16 Results = BinarySearch(Tests,ArrSize,EmpId); if (Results == -1) cout << “id not found”; else cout << “that id was found at index” << Results; PG 486 ~ Pgrm. 8-2
9
9 Binary Search Function int BinarySearch(int Array[ ], int NumElems, int Value) { int First = 0, Last = NumElems - 1, Middle; while (First <= Last) { Middle = First + (Last - First) /2; if (Array[Middle] == Value) return Middle; else if (Array[Middle] > Value) Last = Middle -1; else First = Middle + 1; } return -1; } PG 486 ~ Pgrm. 8-2
10
10 1622354457665781011141 [0][12] Middle equals 6 Array[Middle] < Value Value equals 16 [6]
11
11 1622354457665781011141 [0][12] Middle equals 6 Array[Middle] < Value Value equals 16 [6] First = Middle + 1 // 7 Middle = First + (Last - First) /2;
12
12 1622354457665781011141 [0][12] Middle equals 9 Array[Middle] > Value Value equals 16 [6][9] Last = Middle - 1 // 8 Middle = First + (Last - First) /2;
13
13 1622354457665781011141 [0][12] Middle equals 7 Array[Middle] == Value Value equals 16 [6][9] Last = Middle - 1 // 8 Middle = First + (Last - First) /2; [7]
14
14 Sorting Arrays using a Bubble Sort Write a program that contains a function that receives a one- dimensional array and the number of elements in the array and sorts the elements from the lowest to the highest. PG 498 ~ Pgrm. 8-4
15
15 2648101289684537
16
16 4810128968453726 2648101289684537
17
17 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } PG 498 ~ Pgrm. 8-4 2648101289684537 2648101289684537266 Count 0
18
18 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 2648101289684537266 Count 1 PG 498 ~ Pgrm. 8-4
19
19 264810128968453726446 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 Count 1 6 Temp PG 498 ~ Pgrm. 8-4
20
20 2468101289684537268 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 PG 498 ~ Pgrm. 8-4
21
21 24681012896845372810 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 PG 498 ~ Pgrm. 8-4
22
22 246810128968453721012 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 PG 498 ~ Pgrm. 8-4
23
23 246810128968453721289 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 PG 498 ~ Pgrm. 8-4
24
24 2468101289684537289688968 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 PG 498 ~ Pgrm. 8-4
25
25 2468101268894537289458945 int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 PG 498 ~ Pgrm. 8-4
26
26 2468101268458937289378937 First Pass int Swap, Temp; for (int Count=0; Count < (Elems - 1); Count++) { if (Array[Count] > Array[Count+1]) { Temp = Array[Count]; Array[Count]=Array[Count+1]; Array[Count+1]=Temp; Swap = 1; } 2648101289684537 PG 498 ~ Pgrm. 8-4
27
27 24681012684537892 246810126845378924 First Pass 2648101289684537
28
28 24681012684537892 246810126845378946 2648101289684537 First Pass
29
29 24681012684537892 246810126845378968 2648101289684537 First Pass
30
30 24681012684537892 2468101268453789810 2648 1289684537 First Pass
31
31 24681012684537892 24681012684537891012 2648101289684537 First Pass
32
32 24681012684537892 24681012684537891268 2648101289684537 First Pass
33
33 24681012684537892 24681012684537896845 68 2648101289684537 First Pass
34
34 24681012684537892 24681012456837896837 68 2648101289684537 First Pass
35
35 24681012684537892 24681012453768896889 2648101289684537 First Pass Second Pass
36
36 2468101245376889 24681012684537892 246810124537688924 2648101289684537 First Pass Second Pass
37
37 2468101245376889 24681012684537892 246810124537688946 2648101289684537 First Pass Second Pass
38
38 2468101245376889 24681012684537892 246810124537688968 2648101289684537 First Pass Second Pass
39
39 2468101245376889 24681012684537892 2468101245376889810 2648 1289684537 First Pass Second Pass
40
40 2468101245376889 24681012684537892 24681012453768891012 2648101289684537 First Pass Second Pass
41
41 2468101245376889 24681012684537892 24681012453768891245 2648101289684537 First Pass Second Pass
42
42 2468101245376889 24681012684537892 24681012453768894537 45 2648101289684537 First Pass Second Pass
43
43 2468101245376889 24681012684537892 45 2468101237688968 2648101289684537 First Pass Second Pass
44
44 2468101245376889 24681012684537892 24681012374568896889 Third Pass 2648101289684537 First Pass Second Pass
45
45 2468101245376889 24681012684537892 2468101237456889 222444681012374568894 2648101289684537 First Pass Second Pass Third Pass
46
46 2468101245376889 24681012684537892 2468101237456889 46244681012374568896 2648101289684537 First Pass Second Pass Third Pass
47
47 2468101245376889 24681012684537892 2468101237456889 46266 88 101237456889 8 8 2648101289684537 First Pass Second Pass Third Pass
48
48 2468101245376889 24681012684537892 2468101237456889 84628810123745688910 2648 1289684537 First Pass Second Pass Third Pass
49
49 2468101245376889 24681012684537892 2468101237456889 108462 123745688912 2648101289684537 First Pass Second Pass Third Pass
50
50 2468101245376889 24681012684537892 2468101237456889 37 10846212 4568893712 2648101289684537 First Pass Second Pass Third Pass
51
51 2468101245376889 24681012684537892 2468101237456889 453712108462688937453745 2648101289684537 First Pass Second Pass Third Pass
52
52 2468101245376889 24681012684537892 2468101237456889 3712108462688968 45 2648101289684537 First Pass Second Pass Third Pass
53
53 2468101245376889 24681012684537892 2468101237456889 37121084626889 4568 Fourth Pass 2648101289684537 First Pass Second Pass Third Pass
54
54 Q & A
55
STARTING OUT WITH STARTING OUT WITH Conclusion of Class 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.