ADA Continue
ADA Continue
SR
PAGE
NO. PROGRAM TITLES DATE SIGNATURE REMARKS
NO.
Code:
#include<stdio.h>
int main ()
{
int a[10], i, j;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10 - i - 1; j++)
{
if (a[j] > a[j+1])
{
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
ENROLLMENT NO.:221420107010
}
}
}
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}
Output:
Enter 10 Numbers
6
54
77
44
78
12
86
34
22
45
Sorted Array:
6 12 22 34 44 45 54 77 78 86
Time Complexity:
The Time Complexity of Bubble Sort is O (n2).
ENROLLMENT NO.:221420107010
Practical – 3
II. Selection Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using selection sort.
//Input: The array a of size n.
//Output: Sorted array a.
for i in range (n) do
min = i
for j in range (i + 1,n) do
if a[j] < a[min]
j = min
if min != i
swap (a[i], a[min])
print (a)
Code:
#include<stdio.h>
int main ()
{
int a[10], i, j, min;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
for (i = 0; i < 10; i++)
{
min = i;
for (j = i + 1; j < 10; j++)
{
if (a[j] < a[min])
ENROLLMENT NO.:221420107010
min = j;
if (min != i)
{
int t = a[min];
a[min] = a[i];
a[i] = t;
}
}
}
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}
Output:
Enter 10 Numbers
12
34
13
54
22
14
44
56
32
66
Sorted Array:
12 13 14 22 32 34 44 54 56 66
Time Complexity:
The Time Complexity of Selection Sort is O (n2).
ENROLLMENT NO.:221420107010
Practical – 3
III. Insertion Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using insertion sort.
//Input: The array a of size n.
//Output: Sorted array a.
for i = 1 in range (n) do
key = a[i]
j=i-1
while j > 0 and a[j] > key
a[j+1] = a[j]
j=j-1
a[j+1] = key
print (a)
Code:
#include<stdio.h>
int main ()
{
int a[10], i, j, key;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
for (i = 0; i < 10; i++)
{
key = a[i];
j = i - 1;
while (j > 0 && a[j] > key)
{
ENROLLMENT NO.:221420107010
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}
Output:
Enter 10 Numbers
12
45
66
44
79
8
54
65
34
77
Sorted Array:
8 12 34 44 45 54 65 66 77 79
Time Complexity:
The Time Complexity of Insertion Sort is O (n2).
ENROLLMENT NO.:221420107010
Practical – 3
IV. Merge Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using merge sort.
//Input: The array a of size n.
//Output: Sorted array a.
merge (a, f, m, r)
{
p=m-f+1
q=r–m
arrays b[p], c[q]
for i in range (p)
b[i] = a[f + i]
for i in range (q)
c[i] = a[m + 1 + i]
i = 0, j = 0, k = f
while i < p && j < q
if (b[i] <= c[j])
a[k++] = b[i++];
else
a[k++] = c[j++];
while i < p
a[k++] = b[i++];
while j < q
a[k++] = c[j++];
}
ENROLLMENT NO.:221420107010
mergeSort (a, f, r)
{
if f < r
m = (f + r) / 2;
mergeSort (a, f, m);
mergeSort (a, m + 1, r);
merge (a, f, m, r);
}
print (a)
Code:
#include<stdio.h>
void merge (int a[], int f, int m, int r)
{
int i, j, k;
int p = m - f + 1;
int q = r – m;
int b[p], c[q];
for (i = 0; i < p; i++)
b[i] = a[f + i];
for (i = 0; i < q; i++)
c[i] = a[m + 1 + i];
i = 0, j = 0; k = f;
while (i < p && j < q)
{
if (b[i] <= c[j])
a[k++] = b[i++];
else
a[k++] = c[j++];
}
while (i < p)
a[k++] = b[i++];
ENROLLMENT NO.:221420107010
while (j < q)
a[k++] = c[j++];
}
int main ()
{
int a[10], i;
printf (“Enter 10 Numbers\n”);
for (i = 0; i < 10; i++)
{
scanf ("%d",&a[i]);
}
mergeSort (a, 0, 9);
printf (“Sorted Array:\n”);
for (i = 0; i < 10; i++)
{
printf ("%d ",a[i]);
}
}
ENROLLMENT NO.:221420107010
Output:
Enter 10 Numbers
12
34
13
54
22
14
44
56
32
66
Sorted Array:
12 13 14 22 32 34 44 54 56 66
Time Complexity:
The Time Complexity of Merge Sort is O (n log n).
ENROLLMENT NO.:221420107010
Practical – 3
V. Quick Sort
Algorithm:
//Problem Description: This algorithm sorts an array a using quick sort.
//Input: The array a of size n.
//Output: Sorted array a.
partition (a, s, b)
{
p = a[b]
i=s-1
for j = s in range (b)
if a[j] < p
i++
swap (a[i], a[j])
swap (a[i + 1], a[b])
return i + 1
}
quickSort (a, s, b)
{
if s < b
p = partition (a, s, b)
quickSort (a, s, p - 1)
quicksort (a, p + 1, b)
}
print (a)
ENROLLMENT NO.:221420107010
Code:
#include<stdio.h>
int partition (int a[], int s, int b)
{
int p = a[b], i = s - 1, j;
for (j = s; j <= b – 1; j++)
{
if (a[j] < p)
{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i + 1];
a[i + 1] = a[b];
a[b] = t;
return i + 1;
}
int main ()
ENROLLMENT NO.:221420107010
{
int a[10], i;
printf (“Enter 10 Numbers\n”);for (i = 0; i < 10;
i++)
{
scanf ("%d",&a[i]);
}
quickSort (a, 0, 9);
printf (“Sorted Array:\n”);for (i = 0; i < 10;
i++)
{
printf ("%d ",a[i]);
}
}
Output:
Enter 10 Numbers
56
97
65
3
23
66
22
89
43
75
Sorted Array:
3 22 23 43 56 65 66 75 89 97
Time Complexity:
The Time Complexity of Quick Sort is O (n log n).
Codes:
# include<stdio.h>
{ float x[20], tp = 0;
int i, j, u; u = capacity;
x[i] = 0.0;
if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
} }
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("%f\t", x[i]);
} int main() {
int num, i, j;
scanf("%d", &num);
scanf("%f", &capacity);
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
} } }
return(0);
Output:
j=j-w[i]; }
printf("}");
return 0;
}
Output:
enter the number of items
3
enter the weight and profit of the item 1:2 4
enter the weight and profit of the item 2:3 5
enter the weight and profit of the item 3:4 3
enter the capacity of the knapsack 55
The table is
0 0 0 0 0 0
0 0 4 4 4 4
0 0 4 5 5 9
0 0 4 5 5 9
return m[1][n-1];
}
n++;
int arr[n];
printf("Enter dimensions \n");
for(i=0;i<n;i++)
{
printf("Enter d%d :: ",i);
scanf("%d",&arr[i]);
}
return 0;
}
Output:
Enter number of matrices
2
Enter dimensions
Enter d0 :: 12
Enter d1 :: 23
Enter d2 :: 24
Minimum number of multiplications is 6624