Professional Documents
Culture Documents
Searching SortingUpdated
Searching SortingUpdated
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target,size)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target,size)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target,size)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target,size)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target,size)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target,size)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Algorithm Search(array,target,size)
{
array
7 12 5 22 13 32
target = 13 1 2 3 4 5 6
Linear Search Analysis: Best Case
Algorithm Search(array,target,size)
{
7 12 5 22 13 32
target = 7
Linear Search Analysis: Worst Case
Algorithm Search(array,target,size)
{
7 12 5 22 13 32
target = 32
Linear Search Performance
7 12 42 59 71 86 104 212
Binary Search Example
a
search key = 19
0 1
1 5
2
15
3
4 19
5
25
6
7 27
8 29 middle of the array
9 compare a[6] and 19
31
10 19 is smaller than 29 so the next
11 33 search will use the lower half of the array
12 45
55
88
100
Binary Search Pass 2
a
search key = 19
0 1
1 5
2
15 use this as the middle of the array
3
19 Compare a[2] with 19
4
5
25 15 is smaller than 19 so use the top
half for the next pass
27
Binary Search Pass 3
search key = 19
a
3
4 19
5 use this as the middle of the array
25
Compare a[4] with 19
27
25 is bigger than 19 so use the bottom
half
Binary Search Pass 4
search key = 19
a
3
19 use this as the middle of the array
Compare a[3] with 19
Found!!
Binary Search Example
a
search key = 18
0 1
1 5
2
15
3
4 19
5
25
6
7 27
8 29 middle of the array
9 compare a[6] and 18
31
10 18 is smaller than 29 so the next
11 33 search will use the lower half of the array
12 45
55
88
100
Binary Search Pass 2
a
search key = 18
0 1
1 5
2
15 use this as the middle of the array
3
19 Compare a[2] with 18
4
5
25 15 is smaller than 18 so use the top
half for the next pass
27
Binary Search Pass 3
search key = 18
a
3
4 19
5 use this as the middle of the array
25
Compare a[4] with 18
27
25 is bigger than 18 so use the bottom
half
Binary Search Algorithm
look at “middle” element
if no match then
look left (if need smaller) or
right (if need larger)
1 7 9 12 33 42 59 76 81 84 91 92 93 99
Look for 42
The Algorithm
look at “middle” element
if no match then
look left or right
1 7 9 12 33 42 59 76 81 84 91 92 93 99
Look for 42
The Algorithm
look at “middle” element
if no match then
look left or right
1 7 9 12 33 42 59 76 81 84 91 92 93 99
Look for 42
The Algorithm
look at “middle” element
if no match then
look left or right
1 7 9 12 33 42 59 76 81 84 91 92 93 99
Look for 42
The Binary Search Algorithm
Return found or not found (true or false), so it should be a function.
• In addition, the characteristic of a sort pass is the placement of one or more elements in a
sorted list
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
35
Sorting
• Sorting is a process that organizes a collection of data into either ascending or descending order.
• An internal sort requires that the collection of data fit entirely in the computer’s main memory.
• We can use an external sort when the collection of data cannot fit in the computer’s main
memory all at once but must reside in secondary storage such as on a disk.
In Place Sort
◦ The amount of extra space required to sort the data is constant with the input size.
Joe 98 Sam 90
Pat 86 Pat 86
Sam 90 Zöe 86
Zöe 86 Dan 75
37
Unstable sort algorithms
Joe 98 Sam 90
Pat 86 Zöe 86
Sam 90 Pat 86
Zöe 86 Dan 75
38
Types of Sorting Algorithms
There are many, many different types of sorting algorithms, but the primary ones are:
● Merge Sort
● Shell Sort
● Heap Sort
Bubble sort("Bubbling Up" the Largest Element)
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
1 2 3 4 5 6
77 42 35 12 101 5
40
"Bubbling Up" the Largest Element
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
1 2 3 4 5 6
41
"Bubbling Up" the Largest Element
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
1 2 3 4 5 6
42 77 35 Swap 35 77 12 101 5
42
"Bubbling Up" the Largest Element
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
1 2 3 4 5 6
43
"Bubbling Up" the Largest Element
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
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
44
"Bubbling Up" the Largest Element
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
1 2 3 4 5 6
45
"Bubbling Up" the Largest Element
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
1 2 3 4 5 6
42 35 12 77 5 101
46
“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
47
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
48
Bubble sort
Algorithm bubble()
{
for i =1 to n
{
for j=0 to n-i
{
if a[j]>a[j+1]
{
swap(a[j],a[j+1])
}
}
display(a,n); }
49
Analysis of Bubble Sort
The time complexity for each of the cases is given by the following:
Average-case complexity = O(n2)
Best-case complexity = O(n2)
Worst-case complexity = O(n2)
50
Selection Sort
Idea:
◦ Find the smallest element in the array
◦ Exchange it with the element in the first position
◦ Find the second smallest element and exchange it with the element in the second position
◦ Continue until the array is sorted
Disadvantage:
◦ Running time depends only slightly on the amount of order in the file
51
Selection Sort
Void selectionsort()
{
8 4 6 9 2 3 1
for i= 0 to n-2
minpos=i;
if a[j]<a[minpos]
{ minpos = j; }
if (minpos!=i )
temp=a[i]; a[i]=a[minpos];a[minpos]=temp;
}}
52
Example
8 4 6 9 2 3 1 1 2 3 4 9 6 8
1 4 6 9 2 3 8 1 2 3 4 6 9 8
1 2 6 9 4 3 8 1 2 3 4 6 8 9
1 2 3 9 4 6 8 1 2 3 4 6 8 9
53
Analysis of Selection Sort
Best Case: O(n2)
Worst Case:O(n2)
54
Insertion Sort
Idea: like sorting a hand of playing cards
◦ Start with an empty left hand and the cards facing down on the table.
◦ Remove one card at a time from the table, and insert it into the correct position in the left hand
◦ compare it with each of the cards already in the hand, from right to left
55
Insertion Sort
56
Insertion Sort
57
Insertion Sort
58
Insertion Sort input array
5 2 4 6 1 3
sorted unsorted
59
Insertion Sort
60
Insertion Sort
void insertionSort( arr[], n) /* Move elements of arr[0..i-1], that are
{ greater than key, to one position ahead
of their current position */
for (i = 1; i < n; i++) while (j >= 0 && arr[j] > key)
{ {
key = arr[i]; arr[j+1] = arr[j];
j = i-1; j = j-1;
}
arr[j+1] = key;
}
}
61
Analysis of Insertion Sort
If the data is initially sorted, only one comparison is made on each pass so that the
sort time complexity is O(n)
The number of interchanges needed in both the methods is on the average (n2)/4,
and in the worst cases is about (n2)/2
62
Divide-and-Conquer
Divide-and conquer is a general algorithm design paradigm:
◦ Divide: divide the input data S in two disjoint subsets S1 and S2
◦ Recur: solve the subproblems associated with S1 and S2
◦ Conquer: combine the solutions for S1 and S2 into a solution for S
MERGE SORT 63
MergeSort (Example) - 1
MergeSort (Example) - 2
MergeSort (Example) - 3
MergeSort (Example) - 4
MergeSort (Example) - 5
MergeSort (Example) - 6
MergeSort (Example) - 7
MergeSort (Example) - 8
MergeSort (Example) - 9
MergeSort (Example) - 10
MergeSort (Example) - 11
MergeSort (Example) - 12
MergeSort (Example) - 13
MergeSort (Example) - 14
MergeSort (Example) - 15
MergeSort (Example) - 16
MergeSort (Example) - 17
MergeSort (Example) - 18
MergeSort (Example) - 19
MergeSort (Example) - 20
MergeSort (Example) - 21
MergeSort (Example) - 22
Merge Sort Algorithm
Worst Case:O(nlogn)
88
COMPARISON OF ALL SORTING METHODS
89
90
91
92
11/15/2018 DATA STRUCTURES-I 93
11/15/2018 DATA STRUCTURES-I 94