Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.

Similar presentations


Presentation on theme: "Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These."— Presentation transcript:

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

2 Outlines Sorting Arrays Searching Arrays 2

3 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 } 3 Sorting Arrays

4 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... 4 Sorting Arrays …

5 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 5 Bubble sort

6 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 4 6 7 (elements exchanged) Pass 2: 2 3 4 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) 6 Bubble sort …

7 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. 7 Bubble sort …

8 8

9 9

10 10 Bubble sort …

11 11 Bubble sort …

12 12 Bubble sort …

13 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. 13 Selection sort

14 14

15 Pseudocode of selection sort SELECTION_SORT (A) 1. For i ← 0 to n-2 do 2. minj ← i; 3. minx ← A[i] 4. For j ← i + 1 to n do 5. If A[j] < minx then 6. minj ← j 7. minx ← A[j] 8. A[minj] ← A [i] 9. A[i] ← minx 15 Selection sort …

16 16 Selection sort …

17 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 18 Insertion sort …

19 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. WHILE i > 0 and A[i] > key 6. DO A[i +1] ← A[i] 7. i ← i − 1 8. A[i + 1] ← key 19 Insertion sort …

20 20 Insertion sort …

21 21 Insertion sort …

22 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 23 Searching Arrays …

24 24 Searching Arrays …

25 25 Searching Arrays …

26 26 Searching Arrays …


Download ppt "Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These."

Similar presentations


Ads by Google