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

Sorting and Searching

Uploaded by

niyab10039
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Sorting and Searching

Uploaded by

niyab10039
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Sorting Techniques

•Arranging the data in ascending or descending order is known as sorting.

•Sorting is very important from the point of view of our practical life.

•The best example of sorting can be phone numbers in our phones. If, they
are not maintained in an alphabetical order we would not be able to search
any number effectively.

Sorting Methods

1. Bubble sort
2. Selection sort
The user will decide which sorting method can be
used depending on the following conditions:
a) Time required by a programmer for coding a particular
sorting program.

b) The machine time required for running the program.

c) Space required by the program.


1. Bubble sort
•This is one of the most simple algorithm.
•The logic for this sort is that if the numbers are to be arranged in an ascending
order then the largest number will be pushed at the end of the list.
•This process will be continued till all the numbers are placed in the proper
order.
•Every number will be scanned with the succeeding number and they are
swapped if the top number is larger than the succeeding number.
•If the list is scanned once, it is called as a pass.
•Even if the list is passed once only the largest number is pushed at the end the
rest of the numbers are still not placed.
•For this, we may have to repeat this step many times to get the list sorted
properly.
void bubblesort(int a[ ], int n)
{
int i, j, temp;
for(i=0;i<(n-1);i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
2. Selection sort
This method of sorting is faster than the bubble sort as the movement of
data is minimized.
Selection sort is a simple and efficient sorting algorithm that works by repeatedly
selecting the smallest (or largest) element from the unsorted portion of the list and
moving it to the sorted portion of the list.
The algorithm repeatedly selects the smallest (or largest) element from
the unsorted portion of the list and swaps it with the first element of the
unsorted part. This process is repeated for the remaining unsorted portion
until the entire list is sorted.
//Selection sort
void selection_sort(int A[], int n)
#include <stdio.h> {
void selection_sort(int[],int); int i, j, min_index,t;
void main() for(i=0; i<n-1;i++)
{ {
int size, i; min_index=i;
for(j=i+1;j<n;j++)
printf("\n Enter the size of the array: "); {
scanf("%d", &size); if(A[j]<A[min_index])
int a[size]; {
printf("Enter %d integers\n", size); min_index=j;
for (i=0; i<size; i++) //reading data into an array { }
}
scanf("%d",&a[i]); if(min_index!=i)
} {
selection_sort(a,size); t=A[min_index];
printf("\n The sorted array is: "); A[min_index]=A[i];
for(i=0;i<size;i++) A[i]=t;
}
printf("%d\t",a[i]); }
} }
Searching Technique
void binary_search(int A[], int sz, int number)
{
#include <stdio.h>
int low=0, high=sz, mid;
void binary_search(int[],int,int);
while(low<=high)
void main()
{
{
mid=(high+low)/2;
int num, i, size;
if(A[mid]==number)
printf("\n Enter the size of the array: ");
{
scanf("%d", &size);
printf("\n Number found at %d position :",(mid+1));
int a[size];
break;
printf("Enter %d integers\n", size);
}
for (i=0; i<size; i++)
else if(number>A[mid])
{
{
scanf("%d",&a[i]);
low=mid+1;
}
}
printf("\nEnter the number to be searched: ");
else if(number<A[mid])
scanf("%d",&num);
{
binary_search(a,size,num);
high=mid-1;
}
}
}
if(low>high)
{
printf("\n The number not found: ");
}
}
//Program to insert a number
void main() printf("\nEnter a number to be inserted :");
{ scanf("%d", &num);
int num, pos, i, a[50], size; printf("\nEnter the pos:");
printf("\n Enter the size of the array: "); scanf("%d",&pos);
scanf("%d", &size); for(i=n; i>=pos;i--)
printf("Enter %d integers\n", size); {
a[i]=a[i-1];
for (i=0; i<size; i++) }
{ a[i]=num;
scanf("%d",&a[i]); size--;
} for(i=0;i<size;i++)//displaying updated array
{ printf("%d\t",a[i]);}
}
//Program to insert a number using functions
#include <stdio.h>
int insert_ele(int[],int,int,int);
void main() int insert_ele(int A[], int sz, int number, int p)
{ {
int num, pos, i, a[50], size; int i;
printf("\n Enter the size of the array: "); for(i=sz; i>=p;i--)
scanf("%d", &size); {
printf("Enter %d integers\n", size); A[i]=A[i-1];
for (i=0; i<size; i++) }
{ A[i]=number;
scanf("%d",&a[i]); sz++;
} return sz;
printf("\nEnter a number to be inserted :"); }
scanf("%d", &num);
printf("\nEnter the pos:");
scanf("%d",&pos);
size=insert_ele(a,size,num,pos);
for(i=0;i<size;i++)//displaying updated array
{ printf("%d\t",a[i]);}
}
Program for deleting an element for(i=pos; i<size;i++)
{
#include <stdio.h> a[i-1]= a[i];
void main() }
{ size=size-1;
int pos, i, a[50], size; for(i=0;i<size;i++)
printf("\n Enter the size of the array: "); printf("%d\t",a[i]);
scanf("%d", &size); }
printf("Enter %d integers\n", size);
for (i=0; i<size; i++) //reading data into an array with For loop
{
scanf("%d",&a[i]);
}
printf("\nEnter the pos of the number to be deleted: ");
scanf("%d",&pos);
#include <stdio.h> Program for deleting an element
int delete_ele(int[],int,int); Using Functions
void main()
{
int pos, i, a[50], size;
printf("\n Enter the size of the array: "); int delete_ele(int A[], int sz, int p)
scanf("%d", &size); {
printf("Enter %d integers\n", size); int i;
for (i=0; i<size; i++) for(i=p; i<sz;i++)
{ {
scanf("%d",&a[i]); A[i-1]= A[i];
} }
printf("\nEnter the pos of number to be deleted:"); sz=sz-1;
scanf("%d",&pos); return sz;
size=delete_ele(a,size,pos); }
for(i=0;i<size;i++)//displaying updated array
{ printf("%d\t",a[i]);}
}

You might also like