Searching and Sorting Using An Array
Searching and Sorting Using An Array
1
Unit-2 Searching and Sorting 1/18/2019
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
a solution to a solution to
for (c = 0; c < n; c++) subproblem 1 subproblem 2
scanf("%d", &array[c]);
printf("Enter a number to search\n");
a solution to
scanf("%d", &search); the original problem
1. Binary Search
for (c = 0; c < n; c++)
{ Binary search is a method of searching an element in a sorted list
if (array[c] == search) /* If required element is found */ of items
{ Binary search repeatedly divides the sorted list into half list
printf("%d is present at location %d.\n", search, c+1); before searching for an element.
break; Here key element is compare with middle element if key element
is less than middle element than proceed to left section else
} proceed to right section.
} It is Very efficient algorithm for searching in sorted array:
if (c == n) K vs A[0] . . . A[m] . . . A[n-1]
printf("%d isn't present in the array.\n", search); 1) If K = A[m], stop (successful search)
return 0; 2) if K < A[m] Search in A[0..m-1]
} 3) if K > A[m] Search in A[m+1..n-1]
9 Prof. Vishal A. Polara 12 Prof. Vishal A. Polara
2
Unit-2 Searching and Sorting 1/18/2019
Binary Search(continue..)
2. Merge sort
Algorithm
Split array A[0..n-1] into about equal halves and make copies of
Binary Search( A[0….n-1],key) each half in arrays B and C
{ Sort arrays B and C recursively
l 0; r n-1 Merge sorted arrays B and C into array A as follows:
while l r do Repeat the following until no elements remain in one of the arrays:
m (l+r)/2 compare the first elements in the remaining unprocessed portions
if K = A[m] return m of the arrays
else if K < A[m] copy the smaller of the two into A, while incrementing the index
indicating the unprocessed portion of that array
r m-1
Once all elements in one of the arrays are processed, copy the
else remaining unprocessed elements from the other array into A.
l m+1
}
13 Prof. Vishal A. Polara 16 Prof. Vishal A. Polara
Example
123456789
3
Unit-2 Searching and Sorting 1/18/2019
8 3 2 9 7 1 5 4
A[i]p A[i]p
3 8 2 9 1 7 4 5 Exchange the pivot with the last element in the first (i.e., ) sub
array — the pivot is now in its final position
2 3 8 9 1 4 5 7 Sort the two sub arrays recursively
1 2 3 4 5 7 8 9
19 Prof. Vishal A. Polara 22 Prof. Vishal A. Polara
4
Unit-2 Searching and Sorting 1/18/2019
Partitioning Algorithm
• The worst-case is when the pivot always ends up in the first
or last element. That is, partitions the array as unequally as
possible.
• In this case
T(n) T(n1) T(11) n T(n1) n
n (n1) … + 1
n(n 1)/2 (n2)
1 2 3 4 5 7 8 9
Best-Case Complexity
Bubble sort
Repeatedly pass through the array
• The best case is clearly when the pivot always partitions the Swaps adjacent elements that are out of order
• Intuitively, this would lead to a recursive depth of at most lg n Comparison of element is made till the largest element reaches to
the end.
calls
For Example: We compare first element with second element if first
•We can actually prove this. In this case is large than second swap first with second here 4 and 8 get swap
T(n) T(n/2) T(n/2) n (n lg n)
Now again 8 get compare with 6 again swap 6 and 8
Now 8 and 9 get compare since 8 is smaller than 9 , there is no
swapping
Now 9 get compare with all next element in the first step.
Repeat same process till the element comes in an order.
8 4 6 9 2 3 1
27 Prof. Vishal A. Polara 30 Prof. Vishal A. Polara
5
Unit-2 Searching and Sorting 1/18/2019
Example 8 4 6 9 2 3 1
Selection sort algorithm
i=1 j
4 6 8 2 3 1 9 Find the smallest element in the array
i=1 j
Exchange it with the element in the first position
4 6 2 3 1 8 9 Find the second smallest element and exchange it with the
i=1 2 j element in the second position
4 2 3 1 6 8 9 Continue until the array is sorted
i=1 j Disadvantage:
2 3 1 4 6 8 9 Running time depends only slightly on the amount of order
i=1 j in the file
2 1 3 4 6 8 9
i=1 j
i=1
Example:
Algorithm
BUBBLESORT(A) 8 4 6 9 2 3 1 1 2 3 4 9 6 8
for i 0 to i<n-2
1 4 6 9 2 3 8 1 2 3 4 6 9 8
do for j 0 to j<=(n-2-i)
do if A[j] < A[j +1] 1 2 6 9 4 3 8 1 2 3 4 6 8 9
T(n) = c1(n+1) + c2 (n i 1) c3 (n i ) c4
i 1 i 1 i 1 do if A[j] < A[smallest]
n
6
Unit-2 Searching and Sorting 1/18/2019
comparisons
do if A[i] < A[smallest]
c5
n 1
(n j )
then smallest ← i
j 1
n
exchanges exchange A[j] ↔ A[smallest] c6
n 1
j 1
(n j )
c7 n-1
37 Vishal A. Polara
Prof. n 1 n 1 n 1
T ( n) c1 c2 n c3 (n 1) c4 (n j 1) c5 n j c6 n j c7 (n 1) (n 2 )
j 1 j 1 j 2
Thank You