1 / 26

Fundamentals of Programming Session 17

Fundamentals of Programming Session 17. Fall 2013. Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu. These slides have been created using Deitel’s slides . Sharif University of Technology . Outlines . Sorting Arrays Searching Arrays. Sorting Arrays .

aretha
Download Presentation

Fundamentals of Programming Session 17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals of Programming Session 17 Fall 2013 • Instructor: Reza Entezari-Maleki • Email:entezari@ce.sharif.edu • These slides have beencreated using Deitel’s slides Sharif University of Technology

  2. Outlines • Sorting Arrays • Searching Arrays

  3. Sorting Arrays • Sorting data (i.e., placing the data into a particular order such as ascending or descending) is one of the most important computing applications. • Sorting data • Important computing application • Virtually every organization must sort some data • Massive amounts must be sorted • Sorting: an operation that segregates items into groups according to specified criterion. • A = { 3 1 6 2 1 3 4 5 9 0 } • A = { 0 1 1 2 3 3 4 5 6 9 }

  4. Sorting Arrays … • There are many different types of sorting algorithms, but the primary ones are: • Bubble Sort • Selection Sort • Insertion Sort • Merge Sort • Quick Sort • Shell Sort • Heap Sort • Radix Sort • Swap Sort • ...

  5. Bubble sort • Bubble sort (sinking sort) • Several passes through the array • Successive pairs of elements are compared • If increasing order (or identical), no change • If decreasing order, elements exchanged • Repeat these steps for every element

  6. Bubble sort … • Example: • Go left to right, and exchange elements as necessary • One pass for each element • Original: 3 4 2 7 6 • Pass 1: 3 2 46 7(elements exchanged) • Pass 2: 2 34 6 7 • Pass 3: 2 3 4 6 7 (no changes needed) • Pass 4: 2 3 4 6 7 • Small elements "bubble" to the top (like 2 in this example)

  7. Bubble sort … • Swapping variables int x = 3, y = 4; y = x; x = y; • What happened? • Both x and y are 3! • Need a temporary variable • Solution int x = 3, y = 4, temp = 0; temp = x; // temp gets 3 x = y; // x gets 4 y = temp; // y gets 3 • Figure 6.15 sorts the values in the elements of the 10-element array a into ascending order.

  8. Bubble sort …

  9. Bubble sort …

  10. Bubble sort …

  11. Bubble sort …

  12. Bubble sort …

  13. Selection sort • Selection sort • This type of sorting is called "Selection Sort" because it works by repeatedly element. • It works as follows: • First find the smallest in the array and exchange it with the element in the first position, • then find the second smallest element and exchange it with the element in the second position, • and continue in this way until the entire array is sorted.

  14. Selection sort … • Pseudocode of selection sort SELECTION_SORT (A) 1. Fori ← 0 ton-2do2.     minj ← i;3.   minx ← A[i]4.    Forj ← i + 1 to n do5.          If A[j] < minx then6.             minj ← j7.             minx ← A[j]8.      A[minj] ← A [i]9.      A[i] ← minx

  15. Selection sort …

  16. Insertion sort • Insertion sort algorithm somewhat resembles selection sort. • Array is imaginary divided into two parts: sorted one and unsorted one. • At the beginning, sorted part contains first element of the array and unsorted one contains the rest. • At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. • When unsorted part becomes empty, algorithm stops.

  17. Insertion sort …

  18. Insertion sort … • Pseudocode of selection sort INSERTION_SORT (A) 1.     FOR j ← 1 TO length[A] 2.             DO  key ← A[j]    3.                   {Put A[j] into the sorted sequence A[1 . . j − 1]}   4.                    i ← j − 1    5.                    WHILEi > 0 and A[i] > key6.                                 DOA[i +1] ← A[i]            7.                                         i ← i − 1     8.                     A[i + 1] ← key

  19. Insertion sort …

  20. Insertion sort …

  21. Searching Arrays • It may be necessary to determine whether an array contains a value that matches a certain key value. • The process of finding a particular element of an array is called searching. • In this section we discuss two searching techniques—the simple linear search technique and the more efficient (but more complex) binary search technique. • The linear search (Fig. 6.18) compares each element of the array with the search key.

  22. Searching Arrays …

  23. Searching Arrays …

  24. Searching Arrays …

  25. Searching Arrays …

More Related