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

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
One Dimensional Arrays
Week 8 Arrays Part 2 String & Pointer
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Sorting Summary of Sorting Algorithms. _Plan General idea of sorting: we start with an array of items in no particular order, and we want to convert it.
CS0007: Introduction to Computer Programming Array Algorithms.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures.
Searching Arrays Linear search Binary search small arrays
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Chapter 6: Arrays: Lists and Tables
Nested LOOPS.
CSC 211 Data Structures Lecture 13
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CS 108 Notes for March 1, #include //My file 1.c int main (void) { char c = 'z' ; char c_array [10] ; int i = 666 ; int i_array [10] ; float f =
Repetition statements
ARRAYS.
The Bubble Sort Mr. Dave Clausen La Cañada High School
Introduction to Search Algorithms
REPETITION STATEMENTS
FUNCTIONS EXAMPLES.
Siti Nurbaya Ismail Senior Lecturer
Introduction to Search Algorithms
Searching and Sorting Arrays
Searching and Sorting 1-D Arrays
Functions with arrays.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
REPETITION STATEMENTS
Presentation transcript:

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

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

+ 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

+ 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

+ 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

+ 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

+ 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

+ 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

+ 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

+ 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] 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? 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

+ 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] x[0]x[1]x[2]x[3] Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan

+ 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

+ 11. Sorting arrays – illustrated x[0]x[1]x[2]x[3] x[0]x[1]x[2]x[3]  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

+ 11. Sorting arrays – illustrated (cnt’d) x[0]x[1]x[2]x[3] x[0]x[1]x[2]x[3]  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

+ 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

+ 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

+ 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

+ 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