fundamentals of programming session 17

Click here to load reader

Upload: aretha

Post on 22-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Fundamentals of Programming Session 17. Fall 2013. Instructor: Reza Entezari-Maleki Email: [email protected]. These slides have been created using Deitel’s slides . Sharif University of Technology . Outlines . Sorting Arrays Searching Arrays. Sorting Arrays . - PowerPoint PPT Presentation

TRANSCRIPT

Fall 2013Instructor: Reza Entezari-Maleki Email: [email protected] University of Technology 1Fundamentals of Programming Session 17These slides have been created using Deitels slides Outlines Sorting Arrays Searching Arrays

22Sorting data (i.e., placing the data into a particular order such as ascending or descending) is one of the most important computing applications. Sorting dataImportant computing applicationVirtually every organization must sort some data Massive amounts must be sortedSorting: 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 }3Sorting Arrays There are many different types of sorting algorithms, but the primary ones are:Bubble SortSelection SortInsertion SortMerge SortQuick SortShell SortHeap SortRadix SortSwap Sort...4Sorting Arrays Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared If increasing order (or identical), no changeIf decreasing order, elements exchangedRepeat these steps for every element

5Bubble sort Example:Go left to right, and exchange elements as necessaryOne pass for each elementOriginal: 3 4 2 7 6Pass 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)

6Bubble sort Swapping variablesint x = 3, y = 4;y = x;x = y;What happened?Both x and y are 3!Need a temporary variableSolutionint x = 3, y = 4, temp = 0;temp = x; // temp gets 3x = y; // x gets 4y = temp; // y gets 3Figure6.15 sorts the values in the elements of the 10-element array a into ascending order.

7Bubble sort 8Bubble sort

9Bubble sort

10Bubble sort

11Bubble sort

12Bubble 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.

13Selection sort14

Pseudocode of selection sortSELECTION_SORT (A) 1. For i 0 to n-2 do2. minj i;3. minx A[i]4. For j i + 1 to n do5. If A[j] < minx then6. minj j7. minx A[j]8. A[minj] A [i]9. A[i] minx

15Selection sort 16Selection 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.17Insertion sort 18Insertion sort

Pseudocode of selection sortINSERTION_SORT (A) 1. FOR j 1 TO length[A] 2. DOkey 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] > key6. DO A[i +1] A[i] 7. i i 1 8. A[i + 1] key

19Insertion sort 20Insertion sort

21Insertion sort

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 techniquesthe 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.

22Searching Arrays23Searching Arrays

24Searching Arrays

25Searching Arrays

26Searching Arrays