Download presentation
Presentation is loading. Please wait.
Published byRoland Bennett Modified over 9 years ago
1
- SEARCHING - SORTING
2
Given: The array The search target: the array element value we are looking for Algorithm: Start with the initial array element (subscript 0) Repeat until there are more array elements (subscript = array size – 1) Compare the target with the current element Dr. Soha S. Zaghloul2
3
Dr. Soha S. Zaghloul3 gpa [0]4.52 gpa [1]3.02 gpa [2]2.25 …… …… gpa [30]3.45 gpa [31]2.99 …… …… gpa [48]4.32 gpa [49]4.82 gpa[0]= = target? gpa[1]= = target? gpa[2]= = target? gpa[30]= = target? gpa[31]= = target? gpa[48]= = target? gpa[49]= = target?
4
Dr. Soha S. Zaghloul4 #include int main (void) { double target, gpa[50]; int i; int index; // position of the found element int found = 0; // flag initially set to 0 // array initialization for (i = 0; i < 50; i++) { printf (“Enter element value> “); scanf (“%f”, &gpa[i]); } // end for // Get the search target printf (“Enter the value you are looking for> “); scanf (“%f”, &target); // Compare all array elements with the target for (i = 0; i < 50; i++) if (gpa[i] == target) { index = i; //store the position found = 1; // set flag to 1 } // end if if (found) // if (found == 1) printf (“Target %f is found at position %d \n”, target, index); else printf (“Target not found”); return 0; } // end main The loop ends when the counter reaches the end of the array
5
Why going through the whole array if the target is found? It is better to update the code fragment in the previous slide as follows: Dr. Soha S. Zaghloul5 int i = 0; int found = 0; while ((!found) && (i < 50)) { if (gpa[i] == target) break; else i++; } // end while int i = 0; int found = 0; while ((!found) && (i < 50)) { if (gpa[i] == target) found = 1; else i++; } // end while
6
Since we need to perform the comparison at least once (with gpa[0]), then linear search can also be implemented using do…while. The core code is as follows: Dr. Soha S. Zaghloul6 int i = 0; int found = 0; do { if (gpa[i] == target) break; else i++; } while ((!found) && (i < 50)) int i = 0; int found = 0; do { if (gpa[i] == target) found = 1; else i++; } while ((!found) && (i < 50))
7
If you want to swap (exchange) the values of two variables, you need a third variable. For example, assume the following: x = 5 y = 8 To swap the values of x and y, we will use a temporary variable temp as follows: Dr. Soha S. Zaghloul7 temp = x; //temp=5 x=5 y=8 x = y;//temp=5 x=8 y=8 y=temp; //temp=5 x=8 y=5
8
Assume the first element is the minimum value Go through all the array and find if there is a lesser value If there is a lesser value put it in minimum Dr. Soha S. Zaghloul8 int i; // counter int array_size;// size of the array int minimum;// to hold the minimum value of the array minimum = x[0]; // assume x[0] is the minimum value in the array for (i = 0; i < array_size; i++) if (x[i] < minimum) minimum = x[i];
9
Dr. Soha S. Zaghloul9 int i, array_size, minimum; minimum = x[0]; for (i = 0; i < array_size; i++) if (x[i] < minimum) minimum = x[i]; x[0]x[1]x[2]x[3] 74458316 minimumi++i < array_size?x[i] < minimum? x[0] = 74: initial value 0: initial value 11 < 4? Truex[1] < minimum? 45 < 74? True array_size = 4 4522 < 4? TrueX[2] < minimum? 83 < 45? False 4533 < 4? TrueX[3] < minimum? 16 < 45? True 1644 < 4? False Exit from loop
10
Sorting an arrays in ascending order is to arrange it such that: X[0] < x[1] < x[2] < x[3] < ….. Example: Assume that we have the following array After being sorted the array will be as follows: Dr. Soha S. Zaghloul10 x[0]x[1]x[2]x[3] 74458316 x[0]x[1]x[2]x[3] 16457483
11
Let current position = 0 Find the minimum element in the array Swap with the current position Move to the next current position (current++) Repeat the same steps until you reach the end of the array Dr. Soha S. Zaghloul11
12
Dr. Soha S. Zaghloul12 x[0]x[1]x[2]x[3] 74458316 x[0]x[1]x[2]x[3] 16458374 1 st iteration current = 0 Minimum element in the array x[0] to x[3] = 16 index_min = 3 (subscript) Swap x[current] with x[index_min] 2 nd iteration current = 1 Minimum element in the array x[1] to x[3] = 45 index_min = current No swap needed
13
Dr. Soha S. Zaghloul13 x[0]x[1]x[2]x[3] 16458374 x[0]x[1]x[2]x[3] 16457483 3 rd iteration current = 2 Minimum element in the array x[2] to x[3] = 74 index_min = 3 (subscript) Swap x[current] with x[index_min] 4 th iteration current = 3 Minimum element in the array x[3] to x[3] = 83 index_min = current No swap needed The last iteration is not needed since only one element is left
14
Dr. Soha S. Zaghloul14 #include int main (void) { // declaration part int x[4], i, current, minimum, index_min, temp; // initialize the array for (i = 0; i < 4; i++) { printf (“enter x[%d]”, i); // displayed as enter x[0] in the 1st iteration scanf (“%d”, &x[i]); } // end for i for (current = 0; current < 3; current++) { // find minimum element in the subarray starting from current // store the subscript of the minimum element (index_min) // if (current != index_min) swap } // end for current } //end main
15
Dr. Soha S. Zaghloul15 for (current = 0; current < 2; current++) // limit = array_size - 2 { minimum = x[current]; // assume x[current] is the minimum index_min = current; // hold the corresponding subscript // find the minimum value in the sub-array starting from x[current] for (i = current; i < 3; i++) { if (x[i] < minimum) { minimum = x[i]; index_min = i; // store the subscript of the minimum element } // end if (x[i] < minimum) } // end for(i = current;… // swap if index_min != current else do nothing if (index_min != current) { temp = x[index_min]; x[index_min] = x[current]; x[current] = temp; } // end if (index_min != current) } // end for (current = 0;… } //end main
16
Dr. Soha S. Zaghloul16 Write a complete program to take two numerical lists of the same length 20. Then, the program stores the lists in arrays x and y; each of which is of length 20. Then, the program should do the following: −Store the product of corresponding elements of x and y in a third array, z, also of size 20. −Display the arrays x, y, z in a three-column table. −Compute and display the square root of the sum of the items in z. Update the above program so that to use a sentinel value to end data entry. The arrays x, y and z are of length 5. Make up your own data and trace your program in the following cases: -The user entered exactly 5 numbers in each list -The user entered 6 numbers in each list -The user entered 3 numbers in each list Make necessary changes to your program accordingly.
17
Dr. Soha S. Zaghloul17 Write a complete program that performs the following to an array x of type int and length 20: -Fill the array with 20 values -Finds and displays the largest value in the array -Finds and displays the subscript of the largest value in the array Write an interactive program that stores a word in an array of characters. The program asks the user to guess a letter. The program should then check if this letter is in word or not. An appropriate message is displayed accordingly. The program ends after 3 times of incorrect guesses or when the user enters a sentinel value.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.