As was pointed out in the lecture, please note the crucial difference between the index and the value at the index. In the program below, min is the index of the minimal, the value is a[min].
/* L-37 MCS 260 Wed 14 Nov 2001 : sinking sort
To sort an array of number in increasing order we build up the
sorted numbers from the beginning of the array, each time appending
to the sorted numbers the minimal element in the rest of the array.
To test the sorting algorithm, we generate a random sequence of
digits. Keeping the numbers so low ensures duplicates. */
void sinking_sort ( int a[], int n );
/* arranges the n elements of a in increasing order using sinking sort */
void test_sort ( int n );
/* calls sinking_sort, generating n random integer numbers */
void print ( int a[], int n );
/* prints the n elements of a on the screen */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ( void )
{
int n;
srand(time(NULL));
printf("Testing sinking sort on random digits.\n");
printf("Give the number of elements : ");
scanf("%d", &n);
test_sort(n);
return 0;
}
static void swap ( int *p, int *q)
/* swaps *p with *q */
{
int tmp = *p;
*p = *q;
*q = tmp;
}
void sinking_sort ( int a[], int n )
{
int i,j,imin;
for (i=0; i<n-1; i++)
{
imin = i; /* initialize minimum as a[i] */
for (j=i+1; j<n; j++) /* look for minimum in a[i+1..n-1] */
if (a[j] < a[imin])
imin = j;
if (imin != i)
swap(&a[i],&a[imin]);
printf("Array after pass %d : \n", i+1);
print(a,n);
}
}
void test_sort ( int n )
{
int a[n];
int i;
for (i=0; i<n; i++)
a[i] = rand() % 10;
printf("The %d random numbers before sorting :\n", n);
print(a,n);
sinking_sort(a,n);
printf("The %d random numbers after sorting :\n", n);
print(a,n);
}
void print ( int a[], int n )
{
int i;
for (i=0; i<n; i++)
printf(" %d", a[i]);
printf("\n");
}
Time was too short to get into bubble sort.
The program below is a nice start to learn about this sorting method.
/* L-37 MCS 260 Wed 14 Nov 2001 : bubble sort
To sort an array of number in increasing order we build up the
sorted numbers from the beginning of the array, each time appending
to the sorted numbers the minimal element in the rest of the array.
Instead of explicitly looking for the minimal element like in sinking
sort, the minimal element "bubbles up" by comparing adjacent elements
and swapping them if necessary.
To test the sorting algorithm, we generate a random sequence of
digits. Keeping the numbers so low ensures duplicates. */
void bubble_sort ( int a[], int n );
/* arranges the n elements of a in increasing order using bubble sort */
void test_sort ( int n );
/* calls sinking_sort, generating n random integer numbers */
void print ( int a[], int n );
/* prints the n elements of a on the screen */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main ( void )
{
int n;
srand(time(NULL));
printf("Testing bubble sort on random digits\n");
printf("Give the number of elements : ");
scanf("%d", &n);
test_sort(n);
return 0;
}
static void swap ( int *p, int *q)
/* swaps *p with *q */
{
int tmp = *p;
*p = *q;
*q = tmp;
}
void bubble_sort ( int a[], int n )
{
int i,j;
for (i=0; i<n-1; i++)
{
for (j=n-1; j>i; j--)
if (a[j-1] > a[j])
swap(&a[j-1],&a[j]);
printf("Array after pass %d : \n", i+1);
print(a,n);
}
}
void test_sort ( int n )
{
int a[n];
int i;
for (i=0; i<n; i++)
a[i] = rand() % 10;
printf("The %d random numbers before sorting :\n", n);
print(a,n);
bubble_sort(a,n);
printf("The %d random numbers after sorting :\n", n);
print(a,n);
}
void print ( int a[], int n )
{
int i;
for (i=0; i<n; i++)
printf(" %d", a[i]);
printf("\n");
}