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

4 code

The document contains three programming assignments focused on sorting algorithms in C. The first assignment implements selection sort, the second uses bubble sort, and the third employs merge sort. Each section includes code snippets for inputting numbers, sorting them, and displaying the sorted results.

Uploaded by

u22ee078
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

4 code

The document contains three programming assignments focused on sorting algorithms in C. The first assignment implements selection sort, the second uses bubble sort, and the third employs merge sort. Each section includes code snippets for inputting numbers, sorting them, and displaying the sorted results.

Uploaded by

u22ee078
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name -himani gupta

Admission no -u22ee078

ASSIGNMENT 6
QUES 1
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);

for(i = 0; i < n - 1; i++)


{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}

printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
return 0;
}
OUTPUT

QUES 2
#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}

OUTPUT

QUES 3
#include <stdio.h>
#define max 7

int a[7] = { 38,27,43,3,9,82,10 };


int b[7];

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]);
}
OUTPUT

You might also like