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

Searching and Sorting Using An Array

The document discusses the characteristics of arrays, including that arrays store elements of the same data type sequentially in memory. It describes one-dimensional, two-dimensional, and multi-dimensional arrays. Searching and sorting algorithms like sequential search, binary search, merge sort, quick sort, bubble sort and selection sort are then covered. The advantages of arrays include fast sorting and searching, while disadvantages include fixed size, wasted memory, and slow insertion and deletion.

Uploaded by

Foram Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Searching and Sorting Using An Array

The document discusses the characteristics of arrays, including that arrays store elements of the same data type sequentially in memory. It describes one-dimensional, two-dimensional, and multi-dimensional arrays. Searching and sorting algorithms like sequential search, binary search, merge sort, quick sort, bubble sort and selection sort are then covered. The advantages of arrays include fast sorting and searching, while disadvantages include fixed size, wasted memory, and slow insertion and deletion.

Uploaded by

Foram Patel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit-2 Searching and Sorting 1/18/2019

The following are the main characteristics


of array:-
 It is a collection of data which has same data types.
 All data items known as elements of an array.
Unit-2 Searching and Sorting  Each element has assigned a unique value which is called address of
particular one item.
 It starts with 0 index and end one less than(n-1) size of array.
 Total memory allocation will depend on no. of elements and data
types of array.
Prepared By:
 Elements always stored sequentially or linear.
Prof.Vishal A. Polara  Lower bound of array (a[0]) will never be changed but upper bound
(Assistant Professor) will be changed.
IT Department  Insertion & Deletion operation will be slower.
 Sorting & searching operation will be faster.
BVM Engg. College
 When array will declare it contain garbage value.
1 Prof. Vishal A. Polara 4 Prof. Vishal A. Polara

Outline Disadvantage of linear array


 Introduction to array  The size of array must be known first before it creates.
 Types of array Otherwise overflow or underflow errors may be
 Operation on an array Searching and sorting possible.
 Sequential search and Binary search  Waste of memory space occurs. If we declare array
 Sorting: Merge sort, Quick sort, Bubble sort, Selection sort, inta[100] and enter only 20 elements. Then 80 elements
having garbage value and occupied memory space.
 Insertion and Deletion operations are very time
consuming and it also disturb rest of element of an array.
 Swapping process required for insertion or deletion.
 Array dimension will be changed after declaration.

2 Prof. Vishal A. Polara 5 Prof. Vishal A. Polara

Introduction to array Types of array


 Array is collection of element of same type.  One dimension array
 It is an ordered set of a fixed number of objects or elements.  int a[10]
 It is also known as derived data type.  Two dimension array
 Array can be declared by following notation: 0 1 2 Data_TypeArray_name [Row][Column];
int a[i][j];
 int a[10] will create array if 10 elements. 0 1 2 3
int a[3][3];
1 4 5 6 Here,
2 7 8 9 Row : 0,1,2 (i)
Column : 0,1,2 (j)

 Multi dimension array

3 Prof. Vishal A. Polara 6 Prof. Vishal A. Polara

1
Unit-2 Searching and Sorting 1/18/2019

Sequential Search Divide and conquer


 When element are order in linear manner and searching
 It has following step
operation performed on it known as sequential search.
 Algorithm: - Divide: Problem is divided into smaller sub problems.

 Step 1: Set i to 1 - Conquer: These sub problems are solved independently.


 Step 2: if i > n then go to step 7 - Combine: Combining all sub problem solution to get final
 Step 3: if A[i] = x then go to step 6 solution.
 Step 4: Set i to i + 1 Example, Binary search, quick sort, Merge sort
 Step 5: Go to Step 2
 Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
 Step 8: Exit

7 Prof. Vishal A. Polara 10 Prof. Vishal A. Polara

Divide-and-Conquer Technique (cont.)


 #include <stdio.h>
a problem of
 int main() size n
 {
 int array[100], search, c, n;
subproblem 1 subproblem 2
 printf("Enter number of elements in array\n"); of size n/2 of size n/2

 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

8 Prof. Vishal A. Polara 11 Prof. Vishal A. Polara

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

14 Prof. Vishal A. Polara 17 Prof. Vishal A. Polara

Analysis of Binary Search


 Time complexity of binary search algorithm
When n=9 number of search = 3
So to find 3 we have to used log of 9 so take log of 9 and you will
get logn.

 Worst case = Θ(log n)


 Best case = Θ(1)
 Average Case = Θ(log n)

 It is limited to array, we can’t use for link list


15 Prof. Vishal A. Polara 18 Prof. Vishal A. Polara

3
Unit-2 Searching and Sorting 1/18/2019

Merge sort Example 3. Quick Sort


 Select a pivot (partitioning element) – here, the first element
8 3 2 9 7 1 5 4 Level 1
 Rearrange the list so that all the elements in the first s positions
are smaller than or equal to the pivot and all the elements in the
Level 2
8 3 2 9 7 1 5 4
remaining n-s positions are larger than or equal to the pivot.
8 3 2 9 71 5 4 Level 3 p

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

Analysis of Merge sort Steps of quick sort


 Here 3 levels are there and 8 elements per level so , total 1 Put i index at first element and j index at n + 1 element
operation 8 * 3 = n * ? 2 Start the journey of i and stop when i element value is greater
 Now find out value of 3 in terms of mathematical form, to find than pivot element.
out mathematical form we can simple take log of n , so we get
log n = 3. 3 Start the journey of j once i stop and stop the journey of j when j
element value is less than pivot.
 So total operations= 8 * (log 8)
= n * (log n) 4 Replace i and j and repeat step 2, 3 and 4.
= Θ(n log n) 5 Replace j with pivot element when i and j crossed each other.
T(n) = 2T(n/2) + Θ(n), T(1) = 0

 All cases have same efficiency: Θ(n log n)


 Space requirement: Θ(n) (not in-place)
20 Prof. Vishal A. Polara 23 Prof. Vishal A. Polara

 Advantage: Quick Sort Algorithm


- When stability is required , sorting link lists
 Quicksort (A[l…r])
- When random access is much more expensive than sequential
access. If l<r
// S is the split portion
S Partition (A[l…r])
//sort the left half
Quicksort(A[l…S-1])
//sort the right half
Quicksort (A[S+1…r])

21 Prof. Vishal A. Polara 24 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(n1)  T(11)  n  T(n1)  n
 n  (n1)  … + 1
 n(n  1)/2  (n2)

25 Prof. Vishal A. Polara 28 Prof. Vishal A. Polara

Quick Sort Example Analysis of Quick sort


5 3 1 9 8 2 4 7  Best case: split in the middle — Θ(n log n)
 Worst case: sorted array! — Θ(n2)
2 3 1 4 5 8 9 7  Average case: random arrays — Θ(n log n)
 Improvements:
1 2 3 4 5 7 8 9
 better pivot selection: median of three partitioning
1 2 3 4 5 7 8 9  switch to insertion sort on small sub files
 elimination of recursion
1 2 3 4 5 7 8 9 These combine to 20-25% improvement

1 2 3 4 5 7 8 9

26 Prof. Vishal A. Polara 29 Prof. Vishal A. Polara

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

array equally.  Here we place largest element at the end of list.

• 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

31 Prof. Vishal A. Polara 1 2 3 4 6 8 9 34 Prof. Vishal A. Polara

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

then exchange temp= A[j]  A[j+1]


1 2 3 9 4 6 8 1 2 3 4 6 8 9
A[j]  A[j+1]
A[j+1]  temp

32 Prof. Vishal A. Polara 35 Vishal A. Polara


Prof.

Bubble-Sort Running Time


Alg.: BUBBLESORT(A)
Algorithm
for i  1 to length[A] c1  Alg.: SELECTION-SORT(A)
do for j  length[A] downto i + 1 c2 n ← length[A]
Comparisons:  n2/2 do if A[j] < A[j +1] c3 for i ← 1 to n - 1
Exchanges:  n2/2
then exchange A[j]  A[j+1] c4 do smallest ← i
n
for j ← i + 1 to n
 (n  i)
n n

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

= (n) + (c2 + c3 + c4)  (n  i) then smallest ← j


i 1 exchange A[i] ↔ A[smallest]
n n n
n(n  1) n 2 n
where  (n  i )  n   i  n 2 
i 1 i 1 i 1 2
 
2 2
Thus,T(n) = (n2)
36 Prof. Vishal A. Polara
33

6
Unit-2 Searching and Sorting 1/18/2019

Analysis of Selection Sort


Alg.: SELECTION-SORT(A) cost times
n ← length[A] c1 1
for j ← 1 to n - 1 c2 n
do smallest ← j
c3 n-1
n2/2 for i ← j + 1 to n
c4  j 1 (n  j  1)
n 1

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

38 Prof. Vishal A. Polara

You might also like