0% found this document useful (0 votes)
20 views6 pages

Prasit

The document discusses different sorting algorithms in C including insertion sort, selection sort, quick sort and merge sort. Code implementations and explanations are provided for each algorithm.

Uploaded by

prasitbairagi730
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)
20 views6 pages

Prasit

The document discusses different sorting algorithms in C including insertion sort, selection sort, quick sort and merge sort. Code implementations and explanations are provided for each algorithm.

Uploaded by

prasitbairagi730
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/ 6

1) Write a c program for implementing insertion sort.

=> #include<stdio.h>
int main()
{
int arr[30],a;
printf("Enter the size of array");
scanf("%d",&a);
printf("Enter the elements in the array");
for(int i=0;i<a;i++)
{ scanf("%d",&arr[i]);}
printf("The element after sorting\t");
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(arr[i]>arr[j])
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
printf("%d\t",arr[i]);}
printf("%d",arr[a-1]);
return 0;
}

2)
Write
ac

program for implementing selection sort.

=> #include <stdio.h>


int main() {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
int i, 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]);
return 0;
}

3) Write a c program for implementing quick sort.


=> #include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}

4) Write a c program for implementing merge sort.

=> #include <stdio.h>


#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}

while(l1 <= mid)


b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}

void sort(int low, int high) {


int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main() {
int i;

printf("List before sorting\n");

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


printf("%d ", a[i]);

sort(0, max);

printf("\nList after sorting\n");

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


printf("%d ", a[i]);
}

5) Write a c program to implementing hashing technique.

1. #include<stdio.h>
2. #define size 7
3. int array[size];
4. void init()
5. { int i;
6. for(i = 0; i < size; i++)
7. array[i] = -1; }
8. void insert(int val)
9. { int key = val % size;
10. if(array[key] == -1)
11. { array[key] = val;
12. printf("%d inserted at array[%d]\n", val,key);
13. }
14. else
15. {
16. printf("Collision : array[%d] has element %d already!\n",key,array[key]);
17. printf("Unable to insert %d\n",val);
18. } }
19. void del(int val)
20. {
21. int key = val % size;
22. if(array[key] == val)
23. array[key] = -1;
24. else
25. printf("%d not present in the hash table\n",val); }
26. void search(int val)
27. {
28. int key = val % size;
29. if(array[key] == val)
30. printf("Search Found\n");
31. else
32. printf("Search Not Found\n"); }
33. void print()
34. { int i;
35. for(i = 0; i < size; i++)
36. printf("array[%d] = %d\n",i,array[i]); }
37. int main()
38. { init();
39. insert(10);
40. insert(4);
41. insert(2);
42. insert(3);
43. printf("Hash table\n");
44. print();
45. printf("\n");
46. printf("Deleting value 10..\n");
47. del(10);
48. printf("After the deletion hash table\n");
49. print();
50. printf("\n");
51. printf("Deleting value 5..\n");
52. del(5);
53. printf("After the deletion hash table\n");
54. print();
55. printf("\n");
56. printf("Searching value 4..\n");
57. search(4);
58. printf("Searching value 10..\n");
59. search(10);
60. return 0; }

You might also like