- searching - sorting. given: the array the search target: the array element value we are looking...

17
ARRAYS - SEARCHING - SORTING

Upload: roland-bennett

Post on 23-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

ARRAYS- SEARCHING

- SORTING

Page 2: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

Given:The arrayThe 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

1. LINEAR SEARCH

Dr. Soha S. Zaghloul 2

Page 3: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

2. LINEAR SEARCH – ILLUSTRATED

Dr. Soha S. Zaghloul 3

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?

Page 4: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

3. LINEAR SEARCH USING FOR - CODE

Dr. Soha S. Zaghloul 4

#include <stdio.h>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

Page 5: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

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:

4. LINEAR SEARCH USING WHILE

Dr. Soha S. Zaghloul 5

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

Page 6: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

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:

5. LINEAR SEARCH USING DO…WHILE

Dr. Soha S. Zaghloul 6

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))

Page 7: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

If you want to swap (exchange) the values of two variables, you need a third variable.

For example, assume the following:x = 5y = 8

To swap the values of x and y, we will use a temporary variable temp as follows:

6. SWAPPING

Dr. Soha S. Zaghloul 7

temp = x; //temp=5 x=5 y=8x = y; //temp=5 x=8 y=8y=temp; //temp=5 x=8 y=5

Page 8: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

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

7. FINDING MINIMUM ELEMENT

Dr. Soha S. Zaghloul 8

int i; // counterint array_size; // size of the arrayint minimum; // to hold the minimum value of the arrayminimum = x[0]; // assume x[0] is the minimum value in the arrayfor (i = 0; i < array_size; i++) if (x[i] < minimum) minimum = x[i];

Page 9: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

8. FINDING MINIMUM ELEMENT - TRACING

Dr. Soha S. Zaghloul 9

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]

74 45 83 16

minimum i++ i < array_size? x[i] < minimum?

x[0] = 74: initial value

0: initial value1 1 < 4? True x[1] <

minimum?45 < 74? True

array_size = 4

45 2 2 < 4? True X[2] < minimum?83 < 45? False

45 3 3 < 4? True X[3] < minimum?16 < 45? True

16 4 4 < 4? False Exit from loop

Page 10: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

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:

9. SORTING ARRAYS

Dr. Soha S. Zaghloul 10

x[0] x[1] x[2] x[3]

74 45 83 16

x[0] x[1] x[2] x[3]

16 45 74 83

Page 11: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

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

10. SORTING ARRAYS – ALGORITHM

Dr. Soha S. Zaghloul 11

Page 12: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

11. SORTING ARRAYS – ILLUSTRATED

Dr. Soha S. Zaghloul 12

x[0] x[1] x[2] x[3]

74 45 83 16

x[0] x[1] x[2] x[3]

16 45 83 74

1st 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]

2nd iteration

current = 1 Minimum element in the array x[1] to x[3] = 45 index_min = current No swap needed

Page 13: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

11. SORTING ARRAYS – ILLUSTRATED (CNT’D)

Dr. Soha S. Zaghloul 13

x[0] x[1] x[2] x[3]

16 45 83 74

x[0] x[1] x[2] x[3]

16 45 74 83

3rd 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]

4th 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

Page 14: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

12. SORTING ARRAYS – CODE

Dr. Soha S. Zaghloul 14

#include <stdio.h>int main (void){// declaration partint x[4], i, current, minimum, index_min, temp;

// initialize the arrayfor (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

Page 15: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

12. SORTING ARRAYS – CODE (CNT’D)

Dr. Soha S. Zaghloul 15

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

Page 16: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

13. SELF-CHECK EXERCISES

Dr. Soha S. Zaghloul 16

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 listMake necessary changes to your program accordingly.

Page 17: - SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array

13. SELF-CHECK EXERCISES

Dr. Soha S. Zaghloul 17

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.