Skip to content

Commit 7c7d3f6

Browse files
committed
a
1 parent 3ee2288 commit 7c7d3f6

File tree

13 files changed

+183
-142
lines changed
  • daa
    • 1. Program for Recursive Binary & Linear Search.
    • 2. Program for Heap Sort.
    • 3. Program for Merge Sort.
    • 4. Program for Selection Sort.
    • 5. Program for Insertion Sort.
    • 6. Program for Quick Sort.
    • 7. Knapsack Problem using Greedy Solution

13 files changed

+183
-142
lines changed
0 Bytes
Binary file not shown.

daa/1. Program for Recursive Binary & Linear Search./rbls.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ int main()
3737
scanf("%d", &a[i]);
3838
printf("Enter the key : ");
3939
scanf("%d", &key);
40-
41-
pos = binary_search(a, 0, n - 1, key);
42-
if (pos == -1)
43-
printf("Key not found");
40+
41+
printf("enter search type 1 for binary search and 2 for linear search : ");
42+
scanf("%d", &i);
43+
44+
if (i == 1)
45+
pos = binary_search(a, 0, n - 1, key);
4446
else
45-
printf("Key found at position %d", pos + 1);
46-
pos = linear_search(a, n, key);
47+
pos = linear_search(a, n, key);
48+
4749
if (pos == -1)
4850
printf("Key not found");
51+
4952
else
53+
5054
printf("Key found at position %d", pos + 1);
5155
return 0;
56+
5257
}

daa/2. Program for Heap Sort./hs

-56 Bytes
Binary file not shown.

daa/2. Program for Heap Sort./hs.c

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,21 @@
44

55
int main()
66
{
7-
int a[100], n, i, j, temp;
8-
9-
printf("The program for the heap sort in c \n written by Chirag Singhal \n roll number 2000330100084 \n");
10-
11-
printf( "Enter the number of elements : " );
12-
13-
14-
scanf( "%d" , &n);
15-
printf( "Enter the elements : " );
16-
17-
18-
for (i = 0; i < n; i++)
19-
scanf( "%d" , &a[i]);
20-
for (i = 0; i < n; i++)
7+
int a[3]={4,3,5}, n, i, j, t;
8+
n=3;
9+
for (i = 1; i < n; i++)
2110
{
22-
for (j = 0; j < n - i - 1; j++)
11+
t = a[i];
12+
j = i - 1;
13+
while (j >= 0 && a[j] > t)
2314
{
24-
if (a[j] > a[j + 1])
25-
{
26-
temp = a[j];
27-
a[j] = a[j + 1];
28-
a[j + 1] = temp;
29-
}
15+
a[j + 1] = a[j];
16+
j--;
3017
}
18+
a[j + 1] = t;
3119
}
32-
printf( "Sorted array is : " );
20+
printf("Sorted elements are: ");
3321
for (i = 0; i < n; i++)
34-
printf( "%d " , a[i]);
35-
36-
printf("\n");
37-
return 0;
22+
printf(" %d", a[i]);
23+
return 0;
3824
}

daa/3. Program for Merge Sort./ms

24 Bytes
Binary file not shown.

daa/3. Program for Merge Sort./ms.c

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,68 @@
22

33
#include<stdio.h>
44

5-
int main()
5+
void merge(int a[], int low, int mid, int high)
66
{
7-
int a[100], n, i, j, temp;
8-
9-
printf("This is the program for the merge sort in c \n written by Chirag Singhal \n roll number 2000330100084 \n");
10-
11-
printf( "Enter the number of elements : " );
12-
scanf( "%d" , &n);
13-
printf( "Enter the elements : " );
14-
for (i = 0; i < n; i++)
15-
scanf( "%d" , &a[i]);
16-
for (i = 0; i < n; i++)
7+
int i, j, k, c[50];
8+
i = low;
9+
k = low;
10+
j = mid + 1;
11+
while (i <= mid && j <= high)
1712
{
18-
for (j = 0; j < n - i - 1; j++)
13+
if (a[i] < a[j])
1914
{
20-
if (a[j] > a[j + 1])
21-
{
22-
temp = a[j];
23-
a[j] = a[j + 1];
24-
a[j + 1] = temp;
25-
}
15+
c[k] = a[i];
16+
k++;
17+
i++;
18+
}
19+
else
20+
{
21+
c[k] = a[j];
22+
k++;
23+
j++;
2624
}
2725
}
28-
printf( "Sorted array is : " );
29-
for (i = 0; i < n; i++)
30-
printf( "%d " , a[i]);
26+
while (i <= mid)
27+
{
28+
c[k] = a[i];
29+
k++;
30+
i++;
31+
}
32+
while (j <= high)
33+
{
34+
c[k] = a[j];
35+
k++;
36+
j++;
37+
}
38+
for (i = low; i < k; i++)
39+
{
40+
a[i] = c[i];
41+
}
42+
}
3143

32-
printf("\n");
33-
return 0;
44+
void merge_sort(int a[], int low, int high)
45+
{
46+
int mid;
47+
if (low < high)
48+
{
49+
mid = (low + high) / 2;
50+
merge_sort(a, low, mid);
51+
merge_sort(a, mid + 1, high);
52+
merge(a, low, mid, high);
53+
}
54+
}
55+
56+
int main()
57+
{
58+
int a[30], n, i;
59+
printf("Enter the number of elements : ");
60+
scanf("%d", &n);
61+
printf("Enter the elements : ");
62+
for (i = 0; i < n; i++)
63+
scanf("%d", &a[i]);
64+
merge_sort(a, 0, n - 1);
65+
printf("Sorted array is : ");
66+
for (i = 0; i < n; i++)
67+
printf(" %d", a[i]);
68+
return 0;
3469
}
80 Bytes
Binary file not shown.
Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
1-
// write a program for the selection sort in c
1+
// C program for implementation of selection sort
2+
#include <stdio.h>
23

3-
#include<stdio.h>
4-
5-
int main()
4+
void s(int *xp, int *yp)
65
{
7-
int a[100], n, i, j, temp;
6+
int t = *xp;
7+
*xp = *yp;
8+
*yp = t;
9+
}
810

9-
printf("This is the program for the selection sort in c \n written by Chirag Singhal \n roll number 2000330100084 \n");
1011

11-
printf( "Enter the number of elements : " );
12-
scanf( "%d" , &n);
13-
printf( "Enter the elements : " );
14-
for (i = 0; i < n; i++)
15-
scanf( "%d" , &a[i]);
16-
for (i = 0; i < n; i++)
12+
void ss(int a[], int n)
13+
{
14+
int i, j, mi;
15+
for (i = 0; i < n-1; i++)
1716
{
18-
for (j = 0; j < n - i - 1; j++)
19-
{
20-
if (a[j] > a[j + 1])
21-
{
22-
temp = a[j];
23-
a[j] = a[j + 1];
24-
a[j + 1] = temp;
25-
}
26-
}
17+
mi = i;
18+
for (j = i+1; j < n; j++)
19+
if (a[j] < a[mi])
20+
mi = j;
21+
s(&a[mi], &a[i]);
2722
}
28-
printf( "Sorted array is : " );
29-
for (i = 0; i < n; i++)
30-
printf( "%d " , a[i]);
3123

24+
}
25+
26+
void pa(int a[], int size)
27+
{
28+
int i;
29+
for (i=0; i < size; i++)
30+
printf("%d ", a[i]);
3231
printf("\n");
33-
return 0;
3432
}
33+
34+
int main()
35+
{
36+
int a[] = {64, 25, 12, 22, 11};
37+
int n = sizeof(a)/sizeof(a[0]);
38+
ss(a, n);
39+
printf("Sorted array: \n");
40+
pa(a, n);
41+
return 0;
42+
}
-48 Bytes
Binary file not shown.
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
// write a `program for the insertion sort in c`
2+
#include <stdio.h>
23

3-
#include<stdio.h>
4-
5-
int main()
4+
void is(int a[], int n) /* function to sort an aay with insertion sort */
65
{
7-
int a[100], n, i, j, temp;
8-
9-
printf("This is the program for the insertion sort in c \nwritten by Chirag Singhal \nroll number 2000330100084 \n");
6+
int i, j, t;
7+
for (i = 1; i < n; i++) {
8+
t = a[i];
9+
j = i - 1;
1010

11-
printf( "Enter the number of elements : " );
12-
scanf( "%d" , &n);
13-
printf( "Enter the elements : " );
14-
for (i = 0; i < n; i++)
15-
scanf( "%d" , &a[i]);
16-
for (i = 0; i < n; i++)
17-
{
18-
for (j = 0; j < n - i - 1; j++)
11+
while(j>=0 && t <= a[j]) /* Move the elements greater than t to one position ahead from their current position*/
1912
{
20-
if (a[j] > a[j + 1])
21-
{
22-
temp = a[j];
23-
a[j] = a[j + 1];
24-
a[j + 1] = temp;
25-
}
13+
a[j+1] = a[j];
14+
j = j-1;
2615
}
16+
a[j+1] = t;
2717
}
28-
printf( "Sorted array is : " );
18+
}
19+
20+
void pa(int a[], int n) /* function to print the array */
21+
{
22+
int i;
2923
for (i = 0; i < n; i++)
30-
printf( "%d " , a[i]);
24+
printf("%d ", a[i]);
25+
}
26+
27+
int main()
28+
{
29+
int a[] = { 12, 31, 25, 8, 32, 17 };
30+
int n = sizeof(a) / sizeof(a[0]);
31+
is(a, n);
32+
printf("\nAfter sorting array elements are - \n");
33+
pa(a, n);
3134

32-
printf("\n");
33-
return 0;
34-
}
35+
return 0;
36+
}

0 commit comments

Comments
 (0)