You are on page 1of 48

DATA STRUCTURES AND ALGORITHMS

Searching And Sorting Algorithms

1
Introduction To Searching
Linear Search
◦ Locate an item by a sequence of comparisons
◦ For example:
◦ Compare each element of array with key value
◦ Useful for small and unsorted arrays

Binary Search
◦ Item being sought – repeatedly compared with items in the list
◦ For example:
◦ Can only be used on sorted arrays
◦ Compares middle element with key
◦ If equal, match found
◦ If key < middle, repeat search through the first half of the array
◦ If key > middle, repeat search through the last half of the array
◦ Very fast; at most n steps, where 2^n > # of elements
◦ 30 element array takes at most 5 steps
◦ 2^5 > 30

Fast Searching
◦ Location of an item is determined directly as a function of the item itself
◦ No hit and trial comparisons
◦ Ideally – Time required to locate an item is constant and does not depend on the number of
items stored
◦ Hash tables and hash functions
2
Example: Linear Search
int linearSearch(int array[], int key, int sizeOfArray)
{
for(int n=0; n<sizeOfArray; n++)
if(a[n]==key)
return element;
else
return -1;
}
Binary search
Binary search - iterative

5
Binary search - recursive

6
Sorting Algorithms
Sorting refers to arranging data in a particular format
Sorting algorithm specifies the way to arrange data in a particular order
Most common orders are numerical or lexicographical order.
Data searching can be optimized to a very high level if data is stored in a
sorted manner
Examples
◦ Telephone Directory
◦ Dictionary
◦ ….

8
Common Sorting Algorithms
Selection Sort
Exchange Sorts
◦ Bubble Sort
◦ Quick Sort
Insertion Sorts
◦ Simple Insertion Sort
◦ Shell Sort
Merge sort
Heap sort

9
Selection Sort

10
Selection sort
The basic idea of a selection sort is to make a number of passes through the
list and, on each pass, select one element to be correctly positioned

Example:
◦ On each pass, the smallest element might be found and then moved to its proper
location.

11
SELECTION SORT
Key Idea:

◦ Find the minimum value in the list

◦ Swap it with the value in the first position

◦ Repeat the steps above for remainder of the list (starting at


the next position)

12
SELECTION SORT
List to be sorted in ascending order: = Minimum

67 33 21 84 49 50 75

67 33 21 84 49 50 75

67 33 21 84 49 50 75

21 33 67 84 49 50 75
Unsorted List

21 33 67 84 49 50 75

21 33 67 84 49 50 75

13
SELECTION SORT
21 33 49 84 67 50 75

21 33 49 84 67 50 75

21 33 49 50 67 84 75

21 33 49 50 67 84 75

21 33 49 50 67 75 84

Sorted List
14
Example: Selection Sort
26 33 43 100 46 88 52 17 53 77

17 | 33 43 100 46 88 52 26 53 77

17 26 | 43 100 46 88 52 33 53 77

17 26 33 | 100 46 88 52 43 53 77

17 26 33 43 | 46 88 52 100 53 77

17 26 33 43 46 | 88 52 100 53 77

17 26 33 43 46 52 | 88 100 53 77

17 26 33 43 46 52 53 | 100 88 77

17 26 33 43 46 52 53 77 | 88 100

17 26 33 43 46 52 53 77 88 | 100

17 26 33 43 46 52 53 77 88 100 |
15
SELECTION SORT
Algorithm:
For i = 1 to n do the following

a. Set smallPos = i.
b. Set smallest = x[smallPos].
c. For j = i+1 to n do the following
If x[j] < smallest
i. Set smallPos = j.
ii. Set smallest = x[smallPos].
d. Set x[smallPos] = x[i].
e. Set x[i] = smallest.

16
SELECTION SORT

17
Exchange Sort

18
Exchange sorts
Unlike selection sorts in which some element is selected and then moved to its
correct position in the list, exchange sorts systematically interchange pairs of
elements that are out of order until eventually no such pairs remain and the
list is therefore sorted.

One common example of an exchange sort is Bubble sort.

19
Bubble sort
Bubble sort is similar to selection sort in the sense that it repeatedly
finds the largest/smallest value in the unprocessed portion of the array
and puts it back

However, finding the largest value is not done by selection

We "bubble" up the largest value instead.

20
BUBBLE SORT
Compares adjacent items and exchanges them if they are out
of order

Comprises of several passes

In one pass, the largest value has been “bubbled” to its


proper position

In second pass, the last value does not need to be compared

21
Bubble Sort
Traverse a collection of elements
◦ Move from the front to the end
◦ “Bubble” the largest value to the end using pair-wise comparisons and
swapping

67 33 21 84 49 50 75

33 67 21 84 49 50 75

33 21 67 84 49 50 75

22
Bubble sort
33 21 67 84 49 50 75

33 21 67 84 49 50 75

33 21 67 49 84 50 75

33 21 67 49 50 84 75

33 21 67 49 50 75 84

The first pass in now complete.

23
Bubble sort
33 21 67 49 50 75 84

After the first pass, the largest element in the list will sink to the end of the list.

33 21 67 49 50 75 84

33 21 67 49 50 75 84

21 33 67 49 50 75 84

21 33 67 49 50 75 84

21 33 49 67 50 75 84
24
Bubble sort
21 33 49 50 67 75 84

21 33 49 50 67 75 84

21 33 49 50 67 75 84

21 33 49 50 67 75 84

.
.
.
25
Bubble
1
Sort
2 3
: example
4 5
2 6

77 42 35 12 101 5

1 2 3 4 5 6

42 Swap42
77 77 35 12 101 5

1 2 3 4 5 6

42 7735 Swap 35
77 12 101 5

26
Bubble
1 2
Sort
3
: example
4 5
2 6

42 35 12 Swap 12
77 77 101 5

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap
1 2 3 4 5 6

42 35 12 77 5 Swap 101
101 5

27
Bubble Sort : example 2
1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed

Notice that only the largest value is correctly placed


All other values are still out of order
So we need to repeat this process

28
“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1

12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101

30
Bubble sort - implementation
for( int i=0; i<n-1; i++)
{
for(int j=0; j<n-1; j++)
if(x[j] > x[j+1])
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}

31
Reducing the Number of
Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101

32
Bubble sort - implementation
for( int i=0; i<n-1; i++)
{
for(int j=0; j < n-i-1; j++)
if(x[j] > x[j+1])
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}

33
Already Sorted Collections?
What if the collection was already sorted?
What if only a few elements were out of place and after a couple of
“bubble ups,” the collection was sorted?

We want to be able to detect this and “stop early”!

1 2 3 4 5 6
5 12 35 42 77 101

34
Using a Boolean “Flag”
We can use a boolean variable to determine if any
swapping occurred during the “bubble up.”

If no swapping occurred, then we know that the


collection is already sorted!

This boolean “flag” needs to be reset after each


“bubble up.”

35
Bubble sort - implementation
void bubbleSort (int S[ ], int length)
{
int hold, j, pass;
bool swapped = TRUE;
for(pass=0; pass<length-1 && swapped==TRUE; pass++)
{
swapped = FALSE;
for(j = 0; j<length-pass-1; j++) {
if(S[j] > S[j+1]){
swapped=TRUE;
hold = S[j];
S[j] = S[j+1];
S[j+1] = hold;
}
}
}
36
Insertion Sorts

37
Simple insertion sort
Insert a new element into a list of already sorted elements so that the
resulting list is still sorted

We divide our array in a sorted and an unsorted array

Initially the sorted portion contains only one element – the first
element in the array.

We take the second element in the array, and put it into its correct
place

38
Simple insertion sort
67 33 21 84 49 50 75

67 33 21 84 49 50 75 Initial sorted sublist of one element

33 67 21 84 49 50 75 Insert 33 to get 2-element sorted sublist

21 33 67 84 49 50 75 Insert 21 to get 3-element sorted sublist

21 33 67 84 49 50 75 Insert 84 to get 4-element sorted sublist

21 33 49 67 84 50 75 Insert 49 to get 5-element sorted sublist

21 33 49 50 67 84 75 Insert 50 to get 6-element sorted sublist

21 33 49 50 67 75 84 Insert 75 to get 7-element sorted sublist

39
Simple insertion sort
36 36 24 10 6 6
24 24 36 24 10 10
10 10 10 36 24 12
6 6 6 6 36 24
12 12 12 12 12 36

40
Simple insertion sort
Search for insertion point from the beginning of the array and shift the
element down to make room for new element

Compare the item at array[current] to one before it, and swap if it is


less.

We then compare array[current-1] to one before it and swap if


necessary

41
Simple insertion sort
for( k = 1; k < n; k++)
{
temp = x[k];
for( i = k-1; i >= 0 && temp < x[i]; i--)
x[i+1] = x[i];
x[i+1] = temp;
}

42
Insertion sort – shell sort
Shell Sort (Diminishing Increment Sort)
Sorts separate subfiles of the original file
Subfiles contain every kth element of the original file

K = 5 , 5 subfiles:

Subfile1 x[0] x[5] x[10] .


Subfile2 x[1] x[6] x[11] . .
Subfile3 x[2] x[7] x[12] . .
Subfile4 x[3] x[8] x[13] . .
Subfile5 x[4] x[9] x[14] . .

43
Shell sort
Sort the k subfiles
Chose a smaller value of k and again partition the file into a new set of
subfiles
Sort each of the larger files
Repeat the process, each time with a smaller value of k, eventually
selecting k = 1, and thus sorting the entire file
A decreasing sequence of increments is fixed at the start of the entire
process and the last value in this sequence must be 1

44
Shell sort
Example:
25 57 48 37 12 92 86 33

Let the sequence be k = (5,3,1).


First Iteration ( k = 5)

(x[0], x[5])
(x[1], x[6])
(x[2], x[7])
(x[3])
(x[4])

45
Shell sort
Second Iteration (k = 3)

(x[0],x[3],x[6])
(x[1],x[4],x[7])
(x[2],x[5])

Third Iteration (k = 1)

(x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7])

46
Shell sort
Original File

25 57 48 37 12 92 86 33

Pass 1
Span = 5

25 57 48 37 12 92 86 33

Pass 2
Span = 3

25 57 33 37 12 92 86 48

25 12 33 37 48 92 86 57
47
Shell sort
Pass 3
Span = 1

25 12 33 37 48 92 86 57

Sorted File

12 25 33 37 48 57 86 92

48
Shell sort
id shellsort(int x[], int n, int incrmnts[], int numinc)

int span, y;

for(int incr = 0; incr < numincr; incr++)


{
/*Span is the size of increment*/
span = incrmnts[incr];

for(int j = span, j < n; j++)


{
/*Insert x[j] at its proper location within its subfile
using simple insert sort*/
y = x[j];
for(int k = j – span;k>=0 && y<x[k]; k-=span)
x[k+span] = x[k];
x[k+span] = y;
}
49
}
Execution times Insertion Sort

Time as function of list size


More to come – Algorithmic complexity
analysis
Insertion sort a good choice for lists of few
thousand elements

Bubble Sort Selection Sort

50

You might also like