Skip to content

Commit 4bd38a2

Browse files
committed
added quick sort and insertion sort
1 parent 1d9f03f commit 4bd38a2

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<stdio.h>
2+
3+
int main(void)
4+
5+
{
6+
int array[25], n, temp, j;
7+
8+
printf("Enter the number of entries: ");
9+
scanf("%d", &n);
10+
printf("Enter the %d elements: ", n);
11+
//user-input
12+
for(int i = 0; i < n; i++)
13+
{
14+
scanf("%d", &array[i]);
15+
}
16+
//The main algo of insertion-sort
17+
//it considers first to be smallest then compares, if other number is found smaller then the choosen one then that number is taken out
18+
//now shift the sorted elemnt according to how smaaler the number is taken out and so on...
19+
for(int i = 1; i < n; i++)
20+
{
21+
temp = array[i];
22+
j = i - 1;
23+
while((temp < array[j]) && (j >= 0))
24+
{
25+
array[j + 1] = array[j];
26+
j = j - 1;
27+
}
28+
array[j + 1] = temp;
29+
}
30+
//printing the sorted array
31+
printf("The sorted array is: \n");
32+
for(int i = 0; i < n; i++)
33+
{
34+
printf("%d ", array[i]);
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<h1 align=center>Insertion Sort</h1>
2+
3+
### Note: THE PROGRAM IS IN C
4+
</br>
5+
6+
Here's a brief about Insertion sort. </br>
7+
8+
`The idea of this algorithm is to build your sorted array in place, shifting elements out of the way if necessary to make room as you go.` </br>
9+
10+
### Sample Input-1
11+
```
12+
4
13+
34 21 43 56
14+
```
15+
16+
### Sample Output
17+
The sorted array is: </br>
18+
21 34 43 56
19+
20+
### Sample Input-2
21+
```
22+
10
23+
44 23 56 21 54 65 87 96 54 99
24+
```
25+
26+
### Sample Output
27+
The sorted array is: </br>
28+
21 23 44 54 54 56 65 87 96 99 </br>
29+
30+
Now see the codes for proper understanding of Algorithm.</br>
31+
32+
### Contributed by:
33+
[*Sarthak Luthra*](https://github.com/sarthak-21)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int values[] = { 88, 56, 100, 2, 25 };
5+
//you can take user input as well
6+
7+
//comparison function
8+
int cmpfunc (const void * a, const void * b) {
9+
return ( *(int*)a - *(int*)b );
10+
}
11+
12+
int main () {
13+
int n;
14+
printf("Before sorting the list is: \n");
15+
for( n = 0 ; n < 5; n++ ) {
16+
printf("%d ", values[n]);
17+
}
18+
19+
qsort(values, 5, sizeof(int), cmpfunc);
20+
//qsort(void *base, size_t items, size_t size, int (*compar)(const void *, const void*))
21+
22+
/* base - This is the pointer to the fisrt element of array
23+
items - This is the number of elements in the array
24+
size - This is the sizxe in bytes of each element in the array
25+
compar - This is the function that compares two element */
26+
27+
printf("\nAfter sorting the list is: \n");
28+
for( n = 0 ; n < 5; n++ ) {
29+
printf("%d ", values[n]);
30+
}
31+
32+
return(0);
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h1 align=center>Quick Sort</h1>
2+
3+
### Note: THE PROGRAM IS IN C
4+
</br>
5+
6+
Here's a brief about Quick sort. </br>
7+
8+
`Like merge sort, Quick sort is a divide and conquer algorithm.` </br>
9+
`It picks an element as pivot and partition the given array around the picked pivot.` </br>
10+
11+
Now see the codes for proper understanding of Algorithm.</br>
12+
13+
### Contributed by:
14+
[*Sarthak Luthra*](https://github.com/sarthak-21)

0 commit comments

Comments
 (0)