SEP- ADA

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7

PART B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 PROGRAM B6 PROGRAM B7 . . .

 
   2. Write a program to sort a list of N elements using Selection Sort technique.
// Algorithm: SelectionSort
// Input: a[]: Array to be sorted, n: Size of the array
// Output: a[] sorted array in ascending order
Step 1: Read array size n
Step 2: Read array elements a[0] to a[n−1]
Step 3: for i ← 0 to n−2:
Step 4: min ← i
Step 5: for j ← i+1 to n−1:
Step 6: if a[j] < a[min]:
Step 7: min ← j
Step 8: if min ≠ i:
Step 9: swap(a[i], a[min])
Step 10: Print sorted array a[0] to a[n−1]
#include
void main()
{
int a[100], n, i, j, min, temp;
printf("Enter array size: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n-1; i++)
{
min = i;
for(j = i+1; j < n; j++)
{
if(a[j] < a[min])
min = j;
}
if(min != i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
printf("Sorted array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
}