# Sorting & Searching

• Published on
09-Feb-2016

• View
25

0

Embed Size (px)

DESCRIPTION

Sorting & Searching. Geletaw S (MSC, MCITP). Objectives. At the end of this session the students should be able to: Design and implement the following simple sorting algorithms: Bubble sort Insertion sort Selection sort - PowerPoint PPT Presentation

Transcript

Sorting

Sorting & Searching Geletaw S (MSC, MCITP)Objectives

At the end of this session the students should be able to:Design and implement the following simple sorting algorithms:Bubble sortInsertion sortSelection sortCompare the efficiency of the sorting algorithms in terms of Big-O complexity and space requirements to sort small number of elements.Discuss the efficiency of the following simple searching algorithms:Sequential or linear searchBinary search

What is Sorting?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 }Why Sort and ExamplesConsider:Sorting Books in LibrarySorting Individuals by Height Sorting Movies in Best-sellerSorting Numbers (Sequential)Types of Sorting AlgorithmsThere are many, many different types of sorting algorithms, but the primary ones are:

Bubble Sort Selection Sort Insertion Sort Merge Sort Shell Sort Heap Sort Quick Sort Radix Sort Swap SortBubble SortThe simplest algorithm to implement The slowest algorithm on very large inputs.Basic Idea:Loop through array from i=0 to n and swap adjacent elements if they are out of order.

Pseudo code Set flag = falseTraverse the array and compare pairs of two elements 1.1 If E1 E2 - OK1.2 If E1 > E2 thenSwitch(E1, E2) and set flag = trueIf flag = true goto 1.Implementation void bubbleSort (Array S, length n) {boolean isSorted = false;while(!isSorted) {isSorted = true;for(i = 0; i S[i+1]) {int aux = S[i];S[i] = S[i+1]; S[i+1] = aux; isSorted = false;} }}

Bubble Sort1 23 2 56 9 8 10 1001 2 23 56 9 8 10 1001 2 23 9 56 8 10 1001 2 23 9 8 56 10 1001 2 23 9 8 10 56 100---- finish the first traversal -------- start again ----1 2 23 9 8 10 56 1001 2 9 23 8 10 56 1001 2 9 8 23 10 56 1001 2 9 8 10 23 56 100---- finish the second traversal -------- start again ----.Analysis of Bubble Sort

How many comparisons?(n-1)+(n-2)++1= O(n2)How many swaps?(n-1)+(n-2)++1= O(n2)

Selection Sort How does it work: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.

ContSelection sort is:The simplest sorting techniques. a good algorithm to sort a small number of elementsan incremental algorithm induction methodSelection sort is Inefficient for large lists.Incremental algorithms process the input elements one-by-one and maintain the solution for the elements processed so far.

Selection Sort AlgorithmInput: An array A[1..n] of n elements.Output: A[1..n] sorted in nondecreasing order.1. for i 1 to n - 12. k i3. for j i + 1 to n {Find the i th smallest element.}4. if A[j] < A[k] then k j5. end for6. if k i then interchange A[i] and A[k]7. end for13ExampleOriginal array:6 3 5 4 9 2 71st pass -> 2 3 5 4 9 6 7 (2 and 6 were swapped)2nd pass -> 2 3 4 5 9 6 7 (4 and 5 were swapped)3rd pass -> 2 3 4 5 6 9 7 (6 and 9 were swapped)4th pass -> 2 3 4 5 6 7 9 (7 and 9 were swapped)5th pass -> 2 3 4 5 6 7 9 (no swap)6th pass -> 2 3 4 5 6 7 9 (no swap)

Analysis of AlgorithmsAlgorithm analysis: quantify the performance of the algorithm, i.e., the amount of time and space taken in terms of n.T(n) is the total number of accesses made from the beginning of selection_sort until the end.selection_sort itself simply calls swap and find_min_index as i goes from 1 to n-1

= n-1 + n-2 + n-3 + + 1 = n(n-1)/2Or = (n - i) = n (n - 1) / 2 - O(n2)Insertion SortInsertion sort keeps making the left side of the array sorted until the whole array is sorted.

Insertion Sortwhile some elements unsorted:Using linear search, find the location in the sorted portion where the 1st element of the unsorted portion should be inserted Move all the elements after the insertion location up one position to make space for the new element17An insertion sort partitions the array into two regions

18Example: sorting numbered cards231745181222Given some numbered cards.Our aim is to put them into nondecreasing order.

Example: sorting numbered cards231745181222126543Example: sorting numbered cards231745181222126543126543Example: sorting numbered cards231745181222126543126543Example: sorting numbered cards231745181222126543126543Example: sorting numbered cards231745181222126543126543Example: sorting numbered cards231745181222126543126543Example: sorting numbered cards181222172345126543126543Algorithm: INSERTIONSORTInput: An array A[1..n] of n elements.Output: A[1..n] sorted in nondecreasing order.1. for i 2 to n2. x A[i]3. j i - 14. while (j >0) and (A[j] > x)5. A[j + 1] A[j]6. j j - 17. end while8. A[j + 1] x9. end for27An insertion sort of an array of five integers

28Cont. Algorithm Detail A[i] is inserted in its proper position in the ith iteration in the sorted subarray A[1 .. i-1]In the ith step, the elements from index i-1 down to 1 are scanned, each time comparing A[i] with the element at the correct position.In each iteration an element is shifted one position up to a higher index.The process of comparison and shifting continues until:Either an element A[i] is found orWhen all the sorted sequence so far is scanned.Then A[i] is inserted in its proper position.29AnalysisLet a0,...,an-1 be the sequence to be sorted. At the beginning and after each iteration of the algorithm the sequence consists of two parts: the first part a0,...,ai-1 is already sorted, the second part ai,...,an-1 is still unsorted (i in 0,...,n). The worst case occurs when in every step the proper position for the element that is inserted is found at the beginning of the sorted part of the sequence.30Searching Definition The process of finding a particular element of an array Types of SearchSequential or linear searchBinary searchWhy Search & Examples Looking up a phone number Accessing a Web site andChecking the definition of a word in a dictionary

Linear Search (Sequential Search) AlgorithmEach element of an array is read one by one sequentially and it is compared with the search key. Let A be an array of having n elements, A[0],A[1],A[2], ...... A[n-1]. item is the element to be searched. Then this algorithm will find the location loc of item in A. Set loc = 1, if the search is unsuccessful.

Cont.Input an array A of n elements and item to be searched and initialize loc = 1.Initialise i = 0; and repeat through step 3 if (i < n) by incrementing i by one.If (item = A[i])loc = iGOTO step 44. If (loc >= 0)Display item is found and searching is successful5. elseDisplay item is not found and searching is unsuccessful6. exit

Efficiency of Linear Search

The linear search algorithm runs in O(n) timeExample for implementation int Linear_Search(int list[], int key){int index=0;int found=0;Do{if(key==list[index]) found=1;else index++;}while(found==0&&index