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

Quick Sort: Unit 5-Searching, Sorting, Hashing

The document discusses various sorting algorithms like quicksort, mergesort, bubble sort, insertion sort, selection sort, and shell sort. Code implementations for each algorithm are provided along with sample inputs and outputs showing the sorting of an array of numbers. The document also covers searching algorithms like linear search and binary search with code examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Quick Sort: Unit 5-Searching, Sorting, Hashing

The document discusses various sorting algorithms like quicksort, mergesort, bubble sort, insertion sort, selection sort, and shell sort. Code implementations for each algorithm are provided along with sample inputs and outputs showing the sorting of an array of numbers. The document also covers searching algorithms like linear search and binary search with code examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT 5-SEARCHING,SORTING,HASHING

QUICK SORT
#include<stdio.h>
void swap (int a[], int left, int right)
{
int temp;
temp=a[left];
a[left]=a[right];
a[right]=temp;
}//end swap

void quicksort( int a[], int low, int high )


{
int pivot;
// Termination condition!
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
} //end quicksort

int partition( int a[], int low, int high )


{
int left, right;
int pivot_item;
int pivot = left = low;
pivot_item = a[low];
right = high;
while ( left < right )
{
// Move left while item < pivot
while( a[left] <= pivot_item )
left++;
// Move right while item > pivot
while( a[right] > pivot_item )
right--;
if ( left < right )
swap(a,left,right);
}
// right is final position for the pivot
a[low] = a[right];
a[right] = pivot_item;
return right;
UNIT 5-SEARCHING,SORTING,HASHING

}//end partition

void printarray(int a[], int n)


{
int i;
for (i=0; i<n; i++)
printf(" %d ", a[i]);
printf("\n");
}//end printarray

void main()
{
int a[50], i, n;
printf("\nEnter no. of elements: ");
scanf("%d", &n);
printf("\nEnter the elements: \n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
printf("\nUnsorted elements: \n");
printarray(a,n);
quicksort(a,0,n-1);
printf("\nSorted elements: \n");
printarray(a,n);
}//end main

OUTPUT:
Enter no. of elements: 5
Enter the elements:
4
3
7
1
9
Unsorted elements:
4 3 7 1 9
Sorted elements:
1 3 4 7 9
UNIT 5-SEARCHING,SORTING,HASHING

MERGE SORT
#include<stdio.h>
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
while(i<=j1 && j<=j2) //while elements in both lists
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}

while(i<=j1) //copy remaining elements of the first list


temp[k++]=a[i++];

while(j<=j2) //copy remaining elements of the second list


temp[k++]=a[j++];

for(i=i1,j=0;i<=j2;i++,j++) //Transfer elements from temp[] back to a[]


a[i]=temp[j];
}

void partition(int a[],int i,int j)


{
int mid;

if(i<j)
{
mid=(i+j)/2;
partition(a,i,mid); //left recursion
partition(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}

void main()
{
int a[30],n,i;
printf("Enter no of elements:\n");
UNIT 5-SEARCHING,SORTING,HASHING

scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
partition(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);

OUTPUT:
Enter no of elements:
7
Enter array elements:
2
5
9
3
4
8
1
Sorted array is :1 2 3 4 5 8 9
UNIT 5-SEARCHING,SORTING,HASHING

BUBBLE SORT
#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
printf("\n\tenter the size of array:");
scanf("%d",&n);
printf("\n\tEnter the elements of array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\tElements of array before sorting are...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n;i++) /* n-1 passes required to sort the entire array */
{
for(j=0;j<n-i-1;j++) /* Compare adjacent elements and exchange if necessary */
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\tElements of array after sorting are...");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n");
}
OUTPUT:
enter the size of array:5
Enter the elements of array:
66 33 22 88 44
Elements of array before sorting are...
66 33 22 88 44
Elements of array after sorting are...
22 33 44 66 88
UNIT 5-SEARCHING,SORTING,HASHING

INSERTION SORT
#include<stdio.h>
void main()
{
int a[10],p,j,n,tmp;
printf("Enter the number of elements:\n");
scanf("%d",&n);
printf("enter the elements to e sorted:\n");
for(p=0; p<n; p++)
{
scanf("%d",&a[p]);
}
for(p=1; p<n; p++)
{
tmp = a[p];
for(j=p ;j>0 && a[j-1] > tmp ;j--)
{
a[j] = a[j-1];
}
a[j] = tmp;
}
printf("After sorting:\n");
for(p=0; p<n; p++)
{
printf("%d\t",a[p]);
}
}

OUTPUT:
Enter the number of elements:
5
enter the elements to e sorted:
33
22
11
66
44
After sorting:
11 22 33 44 66
UNIT 5-SEARCHING,SORTING,HASHING

LINEAR SEARCHING
#include <stdio.h>
void main()
{
int array[10];
int i, num, keynum, found = 0,note;

printf("Enter the value of num \n");


scanf("%d", &num);
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array is \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be searched \n");
scanf("%d", &keynum);
/* Linear search begins */
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
note=i;
break;
}
}
if (found == 1)
printf("Element is present in the position %d\n",note+1);
else
printf("Element is not present in the array\n");
}

OUTPUT:
Enter the value of num
5
Enter the elements one by one
4
7
2
UNIT 5-SEARCHING,SORTING,HASHING

5
1
Input array is
4
7
2
5
1
Enter the element to be searched
4
Element is present in the position 1
UNIT 5-SEARCHING,SORTING,HASHING

BINARY SEARCHING
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100],i,j,temp;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
/* Bubble sorting begins */
for (i = 0; i < n; i++)
{
for (j = 0; j < (n - i - 1); j++)
{
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


UNIT 5-SEARCHING,SORTING,HASHING

}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);
return 0;
}

OUTPUT:
Enter number of elements
5
Enter 5 integers
2
5
3
7
1
Sorted array is...
1
2
3
5
7
Enter value to find
1
1 found at location 1.
UNIT 5-SEARCHING,SORTING,HASHING

SELECTION SORT
#include<stdio.h>
void selectionsort (int a[], int size)
{
int i, j, min, temp;
for(i=0; i < size-1; i++ )
{
min = i; //setting min as i
for(j=i+1; j < size; j++)
{
if(a[j] < a[min]) //if element at j is less than element at min position
{
min = j; //then set min as j
}
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

void main()
{
int a[30],n,i;
printf("Enter no of elements:\n");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d\t",a[i]);

OUTPUT:
Enter no of elements:
5
Enter array elements:
2 7 4 9 1
Sorted array is : 1 2 4 7 9
UNIT 5-SEARCHING,SORTING,HASHING

SHELL SORT
#include<stdio.h>
void main()
{
int arr[30];
int i,j,k,tmp,num;
printf("Enter total no. of elements :\n ");
scanf("%d", &num);
printf("Enter the elements\n");
for(k=0; k<num; k++)
{
scanf("%d",&arr[k]);
}
for(i=num/2; i>0; i=i/2)
{
for(j=i; j<num; j++)
{
for(k=j-i; k>=0; k=k-i)
{
if(arr[k+i]>=arr[k])
break;
else
{
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
}
}
printf("\n**** After Sorting ****\n");
for(k=0; k<num; k++)
printf("%d\t",arr[k]);
}

OUTPUT:
Enter total no. of elements :
6
Enter the elements
3 8 2 9 5 1
**** After Sorting ****
1 2 3 5 8 9

You might also like