40
Sorting & Searching Geletaw S (MSC, MCITP)

Sorting & Searching

  • Upload
    stew

  • View
    51

  • Download
    1

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

Citation preview

Page 1: Sorting & Searching

Sorting & Searching

Geletaw S (MSC, MCITP)

Page 2: Sorting & Searching

ObjectivesAt 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

– Compare 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 search• Binary search

Page 3: Sorting & Searching

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 }

Page 4: Sorting & Searching

Why Sort and Examples

Consider:

• Sorting Books in Library

• Sorting Individuals by Height

• Sorting Movies in Best-seller

• Sorting Numbers (Sequential)

Page 5: Sorting & Searching

Types of Sorting Algorithms

There 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 Sort

Page 6: Sorting & Searching

Bubble Sort

• The 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.

Page 7: Sorting & Searching

Pseudo code

Set flag = false1. Traverse the array and compare pairs of two

elements 1.1 If E1 E2

- OK1.2 If E1 > E2 then

Switch(E1, E2) and set flag = true

2. If flag = true goto 1.

Page 8: Sorting & Searching

Implementation void bubbleSort (Array S, length n) {

boolean isSorted = false;while(!isSorted) {isSorted = true;for(i = 0; i<n; i++) { if(S[i] > S[i+1]) {int aux = S[i];S[i] = S[i+1]; S[i+1] = aux; isSorted = false;}

}}

Page 9: Sorting & Searching

Bubble Sort

1 1 23 2 56 9 8 10 1002 1 2 23 56 9 8 10 1003 1 2 23 9 56 8 10 1004 1 2 23 9 8 56 10 1005 1 2 23 9 8 10 56 100---- finish the first traversal -------- start again ----6 1 2 23 9 8 10 56 1007 1 2 9 23 8 10 56 1008 1 2 9 8 23 10 56 1009 1 2 9 8 10 23 56 100---- finish the second traversal -------- start again ----………………….

Page 10: Sorting & Searching

Analysis of Bubble Sort

How many comparisons?

– (n-1)+(n-2)+…+1= O(n2)

How many swaps?

– (n-1)+(n-2)+…+1= O(n2)

Page 11: Sorting & Searching

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.

Page 12: Sorting & Searching

Con’t…• Selection sort is:

– The simplest sorting techniques. – a good algorithm to sort a small number of elements– an incremental algorithm – induction method

• Selection 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.

Page 13: Sorting & Searching

Selection Sort Algorithm

Input: 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 for

Page 14: Sorting & Searching

Example

• Original array:

6 3 5 4 9 2 7

• 1st 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)

Page 15: Sorting & Searching

Analysis of Algorithms

• Algorithm 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

1

1

min)(n

i

swapelementfindnT

= n-1 + n-2 + n-3 + … + 1 = n(n-1)/2Or = ∑ (n - i) = n (n - 1) / 2 - O(n2)

Page 16: Sorting & Searching

Insertion Sort

• Insertion sort keeps making the left side of the

array sorted until the whole array is sorted.

Page 17: Sorting & Searching

Insertion Sort

• while 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 element

Page 18: Sorting & Searching

An insertion sort partitions the array into two regions

Page 19: Sorting & Searching

Example: sorting numbered cards

23 17 45 18 12 22

Given some numbered cards.Our aim is to put them into nondecreasing order.

Page 20: Sorting & Searching

Example: sorting numbered cards

23 17 45 18 12 221 2 6543

Page 21: Sorting & Searching

Example: sorting numbered cards

23 17 45 18 12 221 2 6543

1 2 6543

Page 22: Sorting & Searching

Example: sorting numbered cards

23

17 45 18 12 221 2 6543

1 2 6543

Page 23: Sorting & Searching

Example: sorting numbered cards

2317

45 18 12 221 2 6543

1 2 6543

Page 24: Sorting & Searching

Example: sorting numbered cards

2317 45

18 12 221 2 6543

1 2 6543

Page 25: Sorting & Searching

Example: sorting numbered cards

2317 4518

12 221 2 6543

1 2 6543

Page 26: Sorting & Searching

Example: sorting numbered cards

1812 2217 23 45

1 2 6543

1 2 6543

Page 27: Sorting & Searching

Algorithm: 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 for

Page 28: Sorting & Searching

An insertion sort of an array of five integers

Page 29: Sorting & Searching

Con’t…. 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 or

– When all the sorted sequence so far is scanned.

• Then A[i] is inserted in its proper position.

Page 30: Sorting & Searching

Analysis

• Let 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.

Page 31: Sorting & Searching

Searching

• Definition

– The process of finding a particular element of an array

• Types of Search• Sequential or linear search

• Binary search

• Why Search & Examples

– Looking up a phone number

– Accessing a Web site and

– Checking the definition of a word in a dictionary

Page 32: Sorting & Searching

Linear Search (Sequential Search) Algorithm

• Each 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.

Page 33: Sorting & Searching

Con’t….1. Input an array A of n elements and “item” to be searched and

initialize loc = – 1.2. Initialise i = 0; and repeat through step 3 if (i < n) by incrementing

i by one.3. If (item = A[i])

loc = iGOTO step 4

4. If (loc >= 0)Display “item is found and searching is successful”

5. elseDisplay “item is not found and searching is unsuccessful”

6. exit

Page 34: Sorting & Searching

Efficiency of Linear Search

– The linear search algorithm runs in O(n) time

Page 35: Sorting & Searching

Example 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<n);if(found==0) index=-1;return index;

}

Page 36: Sorting & Searching

Binary Search algorithm• Binary search is an extremely efficient algorithm

when it is compared to linear search.

• Binary search technique searches “item” in

minimum possible comparisons.

Assumption

– the given array is a sorted one, otherwise first we

have to sort the array elements.

Page 37: Sorting & Searching

Con’t….

1. Find the middle element of the array (i.e., n/2 is the middle element

if the array or the sub-array contains n elements).

2. Compare the middle element with the data to be searched, and then there are following three cases.a. If it is a desired element, then search is successful.b. If it is less than desired data, then search only the first half of the

array, i.e. the elements which come to the left side of the middle element.

c. If it is greater than the desired data, then search only the second half of the array, i.e., the elements which come to the right side of the middle element.

3. Repeat the same step until an element is found or exhaust the search area.

Page 38: Sorting & Searching

Efficiency

The computational time for this algorithm is

proportional to log2 n,Therefore the time

complexity is O(log n)

Page 39: Sorting & Searching

Example for Implementation

int Binary_Search(int list[],int k){

int left=0;int right=n-1;int found=0;do{

mid=(left+right)/2;if(key==list[mid]) found=1;Else{ if(key<list[mid]) right=mid-1; else

left=mid+1;}

}while(found==0&&left<right);if(found==0) index=-1;else index=mid;return index;

}

Page 40: Sorting & Searching

Individual Assignment • Write a program for sorting (bubble, insertion, selection)

and searching (linear, binary) algorithm?Hint:• Menu

1. Sorting1. Bubble2. Selection3. Insertion

2. Searching 1. Linear2. Binary

Criteria for Evaluation

• Running time

• Proper Indentation

• Proper Declaration and

Function usage