Searching and Sorting in C


Searching and Sorting in C.
Searching :
 Searching is a process of finding an element in an array. If element found result true else result false.
Here for searching an element we will use binary search . This search algorithm works on the principle of divide and conquer. For this algorithm to work properly the data collection should be in sorted form.
Program :
C program to search a given number from a list of numbers entered using binary search.
#include <stdio.h>
#include <conio.h>
int binary_search (int [], int, int);
void main()
{
int i, n, a[20], loc, item;
clrscr ();
printf (“Enter how many numbers you want to input (<20) :”);
scanf (“%d”, &n);
printf (“ - - - - - Enter %d sorted numbers - - - - - \n”, n);
for (i=0; i<n; i++);
scanf (“%d” , &a[i]);
printf (“Enter item to be searched :”);
scanf (“%d”, &item);
loc = binary _search (a, n, item);
if (loc !=-1)
printf (“ Item %d is present at %d position “, item, loc);
else
printf (“Item is not present”, item);
getch ();
}
int binary_search (int x[], int count, int num)
{
int beg, end, mid, loc=-1;
beg = 0;
end = count -1;
while (beg<=end)
{
mid = (beg+end)/2;
if (x[mid] = = num)
{
loc = mid;
return (loc);
}
else if (num>x[mid])
beg = mid +1;
else
end = mid – 1;
}
return (loc)
}
OUTPUT
Enter how many numbers you want to input (<20) : 7
- - - - - Enter 7 sorted integer numbers - - - - -
12    14    20    30    42    56    90
Enter item to be searched : 14
Item is present at 1 position
Explanation:
Here we are using binary search to search an element. First, we enter sorted list of numbers and the element to be searched and then call the function binary_search(). Assuming the beginning element is present at 0th location and the last element to be present at (n-1) location. The calculate the middle element. If the middle element is the element to be searched item its location is returned otherwise change the value of beg and end to locate the element. when condition (beg<end) becomes false and element is not found in the list, loc = -1 is returned.

Sorting :
Sorting is a process of arranging the elements of an array in ascending or descending order.
Here for sorting an element we will use bubble sort. Bubble sort is a simple sorting technique. This sorting technique is comparison based technique  in which each pair of adjacent elements is compared and elements are swapped if they are not inorder.
Program:
C program to sort a given list of numbers entered through a keyboard using bubble sort.
#include <stdio.h>
#include <conio.h>
void bubblesort (int [], int);
void main ()
{
int  i, j, n, temp;
int a[20];
clrscr ();
printf (“Enter how many numbers you want to input (<20) :”);
scanf (“%d”, &n);
printf (“ - - - - - Enter %d numbers - - - - -\n”, n);
for (i=0; i<n; i++)
printf (“%d”, a[i]);
printf (“- - - - - You entered - - - - -\n”);
for (i=0; i<n; i++)
printf (“%d\t”, a[i]);
bubblesort (a,n);
printf (“\n- - - - - Sorted list is - - - - - \n”);
for (i=0; i<n; i++)
printf (“%d\t”, a[i]);
getch ();
}
void bubblesort (int x[], int count)
{
int temp, i, j;
 for (i=0; i<count; i++)      /*controls number of pass*/
for (j=0; j<count-i-1; j++)   /*marks comparisons per pass */
if (x[j] > x[j+1])
{
temp = x[j+1];
x[j+1] = x[j];
x[j] = temp;
}}
OUTPUT
Enter how many numbers you want to input (<20) : 5
- - - - - Enter 5 integer numbers - - - - -
44    33    55    22    11
- - - - - You entered - - - - -
44    33    55    22    11
- - - - - Sorted list is - - - - -
11    22    33    44    55

No comments

Powered by Blogger.