Watching Sky

Sorting

  • C
  • BASIC
C

/* SELECTION SORT (ASCENDING ORDER) */
#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr() ;
    int i, j, n, a[10], least ;
    printf ("Enter the number of elements in the list to be sorted: ") ;
    scanf ("%d", &n) ;
    printf ("Enter the %d element(s) in the list: ",n) ;
    for (i=0 ; i<n ; i++) scanf ("%d", &a[i]) ;
    for (i=0 ; i<n ; i++)
    {
        least = i ;
        for (j=i ; j<n ; j++)
        if (a[j] < a[least]) least=j ;
        { j=a[i] ; a[i]=a[least] ; a[least]=j ; }
        printf ("%d\t", a[i]) ;
    }
    getch();
}

/* BUBBLE SORT ( ASCENDING ORDER ) */
#include<stdio.h>
#include<conio.h>
void main()
{
 int array[10],n,i,j,temp ;
 clrscr() ;
 printf("enter the no. of entries to be sorted:\n") ;
 scanf ("%d",&n) ;
 printf ("enter the entries to be sorted:\n");
 for(i=0; i<n; i++)
 scanf ("%d", &array[i]) ;
 printf ("\n") ;
 for (i=0; i<n; i++)
    {
    for (j=i+1; j<n; j++)
    if (array[i] >array[j])
    { temp=array[i] ;
  array[i]=array[j] ; array[j]=temp ; }
    }
  printf ("%d ",array[i]) ;
 }
 getch();
}

BASIC