You are on page 1of 45

Unit 5

Searching & sorting


Searching an Array
Searching is the process of looking for a specific
element in an array; for example, discovering
whether a certain score is included in a list of
scores.

• There are many techniques for searching an array for a particular value
– Linear or sequential search
– Binary search.

• Sequential search:
Start at the beginning of the array and proceed in sequence until either
the value is found or the end of the array is reached
– Simple
– Compare each element of array with key value
– Useful for small and unsorted arrays
Searching Arrays: Binary Search
• Binary search
– For sorted arrays Condition
– Compares middle element with key
• If equal, match found
• If key < middle, looks in first half of array
• If key > middle, looks in last half
• Repeat
– Very fast;

5
Binary Search Example
a
search key = 19
0 1
1 5
2 15
3 19
4 25
5 27
6 29 middle of the array
7 31 compare a[6] and 19
8 33 19 is smaller than 29 so the next
9 45 search will use the lower half of the array
10 55
11 88
12 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 25
5 27 15 is smaller than 19 so use the top
half for the next pass
Binary Search Pass 3
search key = 19
a

3 19
4 use this as the middle of the array
25
5 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 19
4 25
5 27
6 29 middle of the array
7 31 compare a[6] and 18
8 33 18 is smaller than 29 so the next
9 45 search will use the lower half of the array
10 55
11 88
12 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 25
5 27 15 is smaller than 18 so use the top
half for the next pass
Binary Search Pass 3
search key = 18
a

3 19
4 use this as the middle of the array
25
5 Compare a[4] with 18
27
25 is bigger than 18 so use the bottom
half
Binary Search Pass 4
search key = 18
a

3 19 use this as the middle of the array


Compare a[3] with 18
Does not match and no more elements
to compare.
Not Found!!
first = 0;
last = n - 1;
middle = (first+last)/2;
while (search!=array[mid]&&first <= last)
{
if (array[middle] < search)
first = middle + 1;
else
last = middle - 1;
middle = (first + last)/2;
}
If(search==array[mid])
printf("%d found at location %d.\n", search, middle+1);
else
printf("Not found! %d is not present in the list.\n", search);
Sorting an Array
• Sorting a list of elements is another very common problem (along with
searching a list)
– sort numbers in ascending order
– sort numbers in descending order
– sort strings in alphabetic order
– etc.
• There are many ways to sort a list, just as there are many ways to
search a list
● Bubble Sort
● Selection Sort

● Insertion Sort ●Radix Sort


● Merge Sort ● Swap Sort

●Quick Sort ●Heap Sort

● Shell Sort
Bubble Sort: Idea
• Idea: bubble in water.
– Bubble in water moves upward. Why?
• How?
– When a bubble moves upward, the water from
above will move downward to fill in the space
left by the bubble.
Bubble Sort, also known as Exchange Sort, is a simple sorting algorithm. It
works by repeatedly stepping throughout the list to be sorted, comparing two items
at a time and swapping them if they are in the wrong order. The pass through the
list is duplicated until no swaps are desired, which means the list is sorted.
void bubble_sort(int n,int a[])
{ int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
The selection sort enhances the bubble sort by making only a single
swap for each pass through the rundown. In order to do this, a
selection sort searches for the biggest value as it makes a pass and,
after finishing the pass, places it in the best possible area. Similarly,
as with a bubble sort, after the first pass, the biggest item is in the
right place. After the second pass, the following biggest is set up.
This procedure proceeds and requires n-1 goes to sort n item since
the last item must be set up after the (n-1) th pass.
Selection Sort: Idea
1. We have two group of items:
– sorted group, and
– unsorted group
2. Initially, all items are in the unsorted group.
The sorted group is empty.
– We assume that items in the unsorted group
unsorted.
– We have to keep items in the sorted group
sorted.
Insertion Sort: Idea
• Idea: sorting cards.
–8 | 5 9 2 6 3
–5 8 | 9 2 6 3
–5 8 9 | 2 6 3
–2 5 8 9 | 6 3
–2 5 6 8 9 | 3
–2 3 5 6 8 9 |
Insertion Sort: Idea
1. We have two group of items:
– sorted group, and
– unsorted group
2. Initially, all items in the unsorted group and the sorted
group is empty.
– We assume that items in the unsorted group unsorted.
– We have to keep items in the sorted group sorted.
3. Pick any item from, then insert the item at the right
position in the sorted group to maintain sorted property.
4. Repeat the process until the unsorted group becomes
empty.
Insertion Sort: Example

40 2 1 43 3 65 0 -1 58 3 42 4

2 40 1 43 3 65 0 -1 58 3 42 4

1 2 40 43 3 65 0 -1 58 3 42 4
Insertion Sort: Example

1 2 40 43 3 65 0 -1 58 3 42 4

1 2 3 40 43 65 0 -1 58 3 42 4

1 2 3 40 43 65 0 -1 58 3 42 4
Insertion Sort: Example

1 2 3 40 43 65 0 -1 58 3 42 4

0 1 2 3 40 43 65 -1 58 3 42 4

-1
0 1
0 2
1 3
2 40
3 40
43 43
65 65 58 3 42 4
Insertion Sort: Example
-1
0 1
0 2
1 3
2 40
3 40
43 43
65 58 65 3 42 4

-1
0 1
0 2
1 3
2 40
3 43
3 40
65 43
43 58 58
65 65 42 4

-1
0 1
0 2
1 3
2 40
3 43
3 40
65 42 43
43 65 58 65 4

-1
0 1
0 2
1 3
2 40
3 43
3 43
65
4 40
42 42
65 43
43 58 58
65 65
Merge Sort:-Merge sort is yet another sorting algorithm that falls under the category of Divide
and Conquer technique. It is one of the best sorting techniques that successfully build a
recursive algorithm.

Divide and Conquer Strategy:-

In this technique, we segment a problem into two halves and solve them individually. After
finding the solution of each half, we merge them back to represent the solution of the main
problem. Suppose we have an array A, such that our main concern will be to sort the subsection,
which starts at index p and ends at index r, represented by A[p..r].

Divide
If assumed q to be the central point somewhere in between p and r, then we will fragment the
subarray A[p..r] into two arrays A[p..q] and A[q+1, r].

Conquer
After splitting the arrays into two halves, the next step is to conquer. In this step, we
individually sort both of the subarrays A[p..q] and A[q+1, r]. In case if we did not reach the
base situation, then we again follow the same procedure, i.e., we further segment these
subarrays followed by sorting them separately.

Combine
As when the base step is acquired by the conquer step, we successfully get our sorted
subarrays A[p..q] and A[q+1, r], after which we merge them back to form a new sorted
Quick sort:-
void quicksort(int number[25],int first,int last)
{ int i, j, pivot, temp; temp=number[pivot];
if(first<last) number[pivot]=number[j]
{ ;
pivot=first; number[j]=temp;
i=first; quicksort(number,first,j-1
j=last; );
while(i<j)
quicksort(number,j+1,last
{
);
while(number[i]<=number[pivot]&&i<last)
i++;
}
while(number[j]>number[pivot]) }
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
• int main()
• {
• int i, count, number[25];
• printf("How many elements u enter?: ");
• scanf("%d",&count);
• printf("Enter %d elements: ", count);
• for(i=0;i<count;i++)
• scanf("%d",&number[i]);
• quicksort(number,0,count-1);
• printf("Order of Sorted elements: ");
• for(i=0;i<count;i++)
• printf(" %d",number[i]);
• return 0;
• }
Thankyou CONTINUE…………

You might also like