Presentation is loading. Please wait.

Presentation is loading. Please wait.

+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.

Similar presentations


Presentation on theme: "+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015."— Presentation transcript:

1 + ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015

2 + outline Linear search Swapping Find minimum element Sorting Self check exercises Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 2

3 + 1. LINEAR SEARCH 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. Zaghloul updated by Rasha M. AL_Eidan 2015 3

4 + 2. LINEAR SEARCH – illustrated 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? Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 4

5 + 3. LINEAR SEARCH using for - code #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 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 5

6 + 4. LINEAR SEARCH using while 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: 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 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 6

7 + 5. LINEAR SEARCH using DO…while 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: 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)) Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 7

8 + 6. swapping 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: temp = x; //temp=5 x=5 y=8 x = y;//temp=5 x=8 y=8 y=temp; //temp=5 x=8 y=5 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 8

9 + 7. finding minimum element 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 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]; Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 9

10 + 8. finding minimum element - tracing 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 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 10

11 + 9. Sorting arrays 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: x[0]x[1]x[2]x[3] 74458316 x[0]x[1]x[2]x[3] 16457483 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 11

12 + 10. Sorting arrays – algorithm 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. Zaghloul updated by Rasha M. AL_Eidan 2015 12

13 + 11. Sorting arrays – illustrated 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 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 13

14 + 11. Sorting arrays – illustrated (cnt’d) 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 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 14

15 + 12. Sorting arrays – code #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 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 15

16 + 12. Sorting arrays – code (cnt’d) 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 Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 16

17 + 13. Self-check exercises 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. Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 17

18 + 13. Self-check exercises 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. Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015 18


Download ppt "+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015."

Similar presentations


Ads by Google