0% found this document useful (0 votes)
136 views

5 - Sorting and Searching Algorithm

The document discusses sorting and searching algorithms, including bubble sort, selection sort, insertion sort, counting sort, linear search, and binary search. It provides examples and pseudocode to explain how each algorithm works and analyzes the time complexity of sorting and searching methods. Visualizations are provided to illustrate the steps involved in different sorting techniques.

Uploaded by

Hard Fucker
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

5 - Sorting and Searching Algorithm

The document discusses sorting and searching algorithms, including bubble sort, selection sort, insertion sort, counting sort, linear search, and binary search. It provides examples and pseudocode to explain how each algorithm works and analyzes the time complexity of sorting and searching methods. Visualizations are provided to illustrate the steps involved in different sorting techniques.

Uploaded by

Hard Fucker
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Data Structures

Lecture : Sorting and Searching Algorithm

http://visualgo.net/
Go to the link for course materials:
https://drive.google.com/open?id=0B88ti1PlrP6qRjVmSkxsOXJGcXM
Scope
• Several sorting algorithms, including:
– bubble sort
– selection sort
– insertion sort
– counting sort

•Searching, includes:
– linear search
– binary search algorithms

• Complexity of the search and sort algorithms

2
Sorting
 Process to convert the Unordered list to Ordered list.

Index 0 1 2 3 4

Unordered 21 20 27 19 16
list

Index 0 1 2 3 4

Ordered 16 19 20 21 27
list

Ascending order
Descending order
3
Bubble Sort
Concept:
 Compare 1st two elements
 If out of order, exchange them to put in order
 Move down one element, compare 2nd and 3rd
elements, exchange if necessary. Continue until end
of array.
 Pass through array again, exchanging as necessary
 Repeat until pass made with no exchanges.

4
Step-by-step example
First Pass:( 5 1 4 2 8 )
( 1 5 4 2 8 ), Swaps since 5 > 1. move on
( 1 4 5 2 8 ), Swap since 5 > 4. move on
( 1 4 2 5 8 ), Swap since 5 > 2. move on
( 1 4 2 5 8 ), does not swap. End of pass#1
2nd Pass: ( 1 4 2 5 8 )
( 1 4 2 5 8 ) does not swap. move on
( 1 2 4 5 8 ), Swap since 4 > 2. move on
( 1 2 4 5 8 ) does not swap. move on
( 1 2 4 5 8 ) does not swap. End of pass#2
5
Now, the array is already sorted, but the algorithm
does not know if it is completed. The algorithm
needs one whole pass without any swap to know it is
sorted.

Third Pass:( 1 2 4 5 8 )
(12458)
(12458)
( 1 2 4 5 8 ) Terminate.

6
Bubble Sort Function

/* Bubble sort for integers */

void SWAP(a,b) { int t; t=a; a=b; b=t; }

void bubble( int a[], int n ) {


int pass, i;
for(pass=1;pass<n;pass++) { // n-1 passes thru the array.
for(i=0;i<(n-1);i++)// From start to the end of
unsorted part
{ // If adjacent items out of order, swap
if( a[i]>a[i+1] )
SWAP(a[i],a[i+1]);
}
}
}

This code will not be able to terminate


the bubble sort operation on the third
pass. Need to modify the algorithm. 7
Example 2:

8
Bubble SortFunction (re-define)
void SWAP(int *a,int *b) { int t; t=*a;
*a=*b; *b=t; }
void bubble( int a[], int n ) {
int pass, j, flag;
for(pass=1;pass<n;pass++) {//break if no swap
flag = 0;
for(j=0;j<(n-pass);j++) { //discard the
last
if( a[j]>a[j+1] ) {
SWAP(&a[j+1],&a[j]); flag = 1;}
}
if (flag==0) break;
} 9

}
Bubble Sort - Tradeoffs
 Benefit:
 Easy to understand and implement

 Disadvantage:
 Inefficient: slow for large arrays

1-10
Selection Sort
 Concept for sort in ascending order:
 Locate smallest element in array.
Exchange it with element in position 0
 Locate next smallest element in array.
Exchange it with element in position 1.
 Continue until all elements are arranged in
order

1-11
Selection Sort - Example

Array numlist contains:

11 2 29 3

1. Smallest element is 2. Exchange 2


with element in 1st position in array:
2 11 29 3

12
Example (Continued)
2. Next smallest element is 3. Exchange 3 with
element in 2nd position in array:

2 3 29 11

3. Next smallest element is 11. Exchange 11


with element in 3rd position in array:

2 3 11 29

13
 For 4 elements:
 How many positions were selected?

 How many selection will be needed for n


number of elements?

14
Min Min
value Index

8 0

5 1

1 3

5 1

3 5

7 2

5 5

8 3

7 5

9 4

8 5
15
A Selection Sort Function:
void selectionSort(int array[], int n)
{
int select, minIndex, minValue;

for (select = 0; select < (n - 1); select++)


{ //select the location and find the minimum value
minIndex = select;
minValue = array[select];
for(int i = select + 1; i < n; i++)
{ //start from the next of selected one to find minimum
if (array[i] < minValue)
{
minValue = array[i];
minIndex = i;
}
}
array[minIndex] = array[select];
array[select] = minValue;
}
} 16
Selection Sort - Tradeoffs
 Benefit:
 More efficient than Bubble Sort, since
fewer exchanges

 Disadvantage:
 May not be as easy as Bubble Sort to
understand

1-17
Insertion Sort

Mark first element as sorted,


Next for each unsorted element
'extract' the element
  for i = last Sorted Index to 0
    if current SortedElement > extracted
Element
       move/shift sorted element to the right by 1
    else: insert extracted element

18
Pseudocode of Insertion Sort

19
Insertion Sort
 To sort array A[0..n-1], sort A[0..n-2] recursively and
then insert A[n-1] in its proper place among the sorted
A[0..n-2]
 Usually implemented bottom up (non-recursively)

Example: 6, 4, 1, 8, 5
0 1 2 3 4
6 |4 1 8 5
4 6|1 8 5
1 4 6|8 5
1 4 6 8|5
1 4 5 6 8
20
Example: The following table shows the steps for
sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each
step, the item under consideration is underlined. The
item that was moved (or left in place because it was
biggest yet considered) in the previous step is shown
in bold.
37495261
37495261
34795261
34795261
34579261
23457961
23456791
12345679 21
Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It
works by counting the number of objects having distinct key values. Then
doing some arithmetic to calculate the position of each object in the output
sequence

Time Complexity: O(n+k) where n is the number of elements in input array


and k is the range of input.
Auxiliary Space: O(n+k)

Points to be noted:
1. Counting sort is efficient if the range of input data is not significantly greater
than the number of objects to be sorted. Consider the situation where the input
sequence is between range 1 to 10K and the data is 10, 5, 10K, 5K.
2. It is not a comparison based sorting. It running time complexity is O(n) with
space proportional to the range of data.
3. Counting sort uses a partial hashing to count the occurrence of the data
object in O(1).
4. Counting sort can be extended to work for negative inputs also.
22
23
Introduction to Search Algorithms

 Search: locate/find an item in a list of


information

 Two algorithms we will examine:


 Linear/Sequential search
 Binary search
Alternate ref: Schaum’s Outline Series:
Theory and Problems of Data Structures by Seymour Lipschutz
24
Linear Search
 Starting at the first element
 this algorithm steps sequentially
through an array
 examine each element until it locates
the value it is searching for.

25
Linear Search - Example

 Array numlist contains:

17 23 5 11 2 29 3
 Searching for the the value 11
 linear search examines 17, 23, 5, and 11 then
stop with getting 11 at index 3 in the
list.
 Searching for the the value 7
 linear search examines 17, 23, 5, 11, 2, 29,
and 3 then stop without getting 7 in the
list.
26
Linear Search

 Algorithm:
 Start search operation for 0 index to last index
 Compare the item to the first element of array
 If item is equal to the array element stop search
and send the found message with the index.
 If item is not found continue the comparison with
the next element by increasing the index by 1
until the last element.
27
A Linear Search Function

int searchList(int list[], int numElems, int value)


{
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if value was found

while (index < numElems && !found)


{
if (list[index] == value) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position; // Return the position, or -1
} 28
Linear Search - Tradeoffs
 Benefits:
 Easy algorithm to understand
 Array can be in any order

 Disadvantages:
 Inefficient (slow): for array of N elements,
examines N/2 elements on average for value
in array, N elements for value not in array

1-29
Binary Search

Requires array elements to be in order (sorted)


1. Divides the array into three sections:
 middle element
 elements on one side of the middle element
 elements on the other side of the middle element
2. If the middle element is the correct value, done.
Otherwise, go to step 1. using only the half of the
array that may contain the correct value.
3. Continue steps 1. and 2. until either the value is
found or there are no more elements to examine
30
Binary Search - Example

31
Binary Search

Set first index = 0.


Set last index = the last subscript in the array.
Set found = false.
Set position = -1.
While found is not true and first is less than or equal to last
Set middle to the subscript half-way between array[first] and array[last].
If array[middle] equals the desired value
Set found to true.
Set position to middle.
Else If array[middle] is greater than the desired value
Set last to middle - 1.
Else
Set first to middle + 1.
End If.
End While.
Return position.
32
A Binary Search Function

int binarySearch(int array[], int n, int value)


{
int first = 0, // First array element
last = n - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag

while (!found && first <= last)


{
middle = (first + last) / 2; // Calculate mid point
if (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
} 33
Binary Search - Tradeoffsç
 Benefits:
 Much more efficient than linear search.
For array of N elements, performs at most
log2N comparisons

 Disadvantages:
 Requires that array elements be sorted

1-34
Ref. link

1. http://visualgo.net/

2. http://www.comp.nus.edu.sg/~stevenha/visualization/index.html

3. http://en.wikipedia.org/wiki/Bubble_sort#mediaviewer/File:Bubble-sort-
example-300px.gif

4. https://www.youtube.com/watch?v=JP5KkzdUEYI

5. http://en.wikipedia.org/wiki/Selection_sort#mediaviewer/File:Selection-
Sort-Animation.gif

35

You might also like