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

Searching Sorting Notes Handwritten

The document contains C programs for implementing various sorting algorithms: 1) Linear search to search an element in an unsorted array. 2) Binary search to search an element in a sorted array. 3) Bubble sort to sort elements in an array using bubble sorting technique. 4) Selection sort to sort elements using selection sorting approach. 5) Insertion sort implementing insertion sorting algorithm. 6) Counting sort using counting sort approach for sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
396 views

Searching Sorting Notes Handwritten

The document contains C programs for implementing various sorting algorithms: 1) Linear search to search an element in an unsorted array. 2) Binary search to search an element in a sorted array. 3) Bubble sort to sort elements in an array using bubble sorting technique. 4) Selection sort to sort elements using selection sorting approach. 5) Insertion sort implementing insertion sorting algorithm. 6) Counting sort using counting sort approach for sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

for i = 0 to N-1

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
C Programs for searching and sorting
//program for linear search
#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int item,i,loc =-1;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

printf("\nEnter an item to be searched in the array\n");


scanf("%d",&item);

for(i=0;i<N;i++)
{
if(arr[i]==item)
{
loc = i;
printf("\nItem found at location = %d\n",loc);
break;
}
}

if(loc==-1)
{
printf("\nItem not found\n");
}
return(0);
}
//program for binary search
#include<stdio.h>

#define N 10

int main()
{
int arr[N];
int item,loc =-1;
int m,i, f = 0, l = N-1;

printf("Enter an sorted array in ascending order of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

printf("\nEnter an item to be searched in the array\n");


scanf("%d",&item);
while(f<=l)
{
m = (f+l)/2;

if(arr[m]==item)
{
loc = m;
printf("\nItem found at location = %d\n",loc);
break;
}
else if(item<arr[m])
{
l = m - 1;
}
else
{
f = m + 1;
}
}
if(loc==-1)
{
printf("\nItem not found\n");
}
return(0);
}
//program for bubble sort
#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,temp;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

printf("\nSorted array after bubble sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for selection sort
#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,temp,min;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

for(i=0;i<N-1;i++)
{
min = i;

for(j=i+1;j<N;j++)
{
if(arr[j]<arr[min])
{
min = j;
}
}
if(min!=i)
{
temp = arr[i];
arr[i] = arr[min]; //swapping minimum element with element at index i
arr[min] = temp;
}
}

printf("\nSorted array after selection sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for insertion sort

#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,key;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

for(i=1;i<N;i++)
{
key = arr[i];
j = i-1;
while(j>=0 && key<arr[j])
{
arr[j+1]=arr[j];
j=j-1;
}

arr[j+1]=key;
}

printf("\nSorted array after insertion sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for counting sort
#include<stdio.h>

#define N 10

int main()
{
int arr[N];
int k,i;
int b[N]; //b is output array

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

k = arr[0];

for(i = 1; i<N; i++) //finding maximum value k = range = [0...k] = max value among the given array numbers
{
if(arr[i]>k)
{
k = arr[i];
}
}

int count[k+1]; //create count array of size k+1 range = [0...k]

//for turbo compiler create dynamically like int * count = (int *) malloc((k+1)*sizeof(int));

for (i = 0; i <=k; i++)


{
count[i] = 0; // Initialize count array with all zeros
}

for(i=0;i<N;i++)
{
count[arr[i]]++; //count array now contains frequencies of all elements
}

for(i=1;i<=k;i++)
{
count[i] = count[i] + count[i-1]; //update count array// cumulitive frequencies
}
for(i=N-1;i>=0;i--) //create an output array
{
b[--count[arr[i]]] = arr[i];
}

for(i = 0; i<N; i++) //copy output array to original array


{
arr[i] = b[i];
}

printf("\nSorted array after counting sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy