Unit 2.sorting

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Selection Sort

Example

#include<stdio.h>
intmain(){
intarr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
inti, j, position, swap;
for(i=0;i<(n -1);i++){
position =i;
for(j =i+1; j < n;j++){
if(arr[position]>arr[j])
position = j;
}
if(position !=i){
swap =arr[i];
arr[i]=arr[position];
arr[position]= swap;
}
}
for(i=0;i< n;i++)
printf("%d\t",arr[i]);
return0;
}
Output
0 2 6 11 12 18 34 45 55 99
Linear Search

Linear search in C to find whether a number is present in an array. If it's present, then at what location it
occurs. It is also known as a sequential search. It is straightforward and works as follows: we compare each
element with the element to search until we find it or the list ends.

Linear search program in C


#include <stdio.h>

int main()
{
int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}

Binary Search Algorithm

Binary search is the search technique that works efficiently on sorted lists. Hence, to search an element into
some list using the binary search technique, we must ensure that the list is sorted.

Binary search follows the divide and conquer approach in which the list is divided into two halves, and the
item is compared with the middle element of the list. If the match is found then, the location of the middle
element is returned. Otherwise, we search into either of the halves depending upon the result produced
through the match.

Working of Binary search

To understand the working of the Binary search algorithm, let's take a sorted array. It will be easy to
understand the working of Binary search with an example.

There are two methods to implement the binary search algorithm -

o Iterative method
o Recursive method

The recursive method of binary search follows the divide and conquer approach.

Let the elements of array are -

Let the element to search is, K = 56

We have to use the below formula to calculate the mid of the array -

1. mid = (beg + end)/2


So, in the given array -

beg = 0

end = 8

mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.

Now, the element to search is found. So algorithm will return the index of the element matched.

#include <stdio.h>
intmain()
{
inti, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i< n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while(low <= high) {
if(array[mid] < key)
low = mid + 1;
elseif(array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return0;
}
Output:

If the key is present:

If Key is not present:


In the program above, We declare i, low, high, mid, n, key, array[100].

 i is used for iteration through the array.


 low is used to hold the first array index.
 high is used to hold the last array index.
 mid holds the middle value calculated by adding high and low and dividing it by 2.
 n is the number of elements in the array.
 key is the element to search.
 array is the array element of size 100.

We first, take in the number of elements the user array needs and store it in n. Next, we take the elements from
the user. A for loop is used for this process. Then, we take the number to be searched from the array and store
it in the key.

Next, we assign 0 to the low variable which is the first index of an array and n-1 to the high element, which is
the last element in the array. We then calculate the mid value. mid = (low+high)/2 to get the middle index of
the array.

There is a while loop which checks if low is less then high to make sure that the array still has elements in it.
If low is greater then, high then the array is empty. Inside the while loop, we check whether the element at the
mid is less than the key value(array[mid] <key). If yes, then we assign low the value of mid +1 because the
key value is greater then mid and is more towards the higher side. If this is false, then we check if mid is equal
to key. If yes, we print and break out of the loop. If these conditions don’t match then we assign high the value
of mid-1, which means that the key is smaller than mid.

The last part checks if low is greater then high, which means there are no more elements left in the array.

Remember, this algorithm won’t work if the array is not sorted.

You might also like