View Single Post
 
Rand() function seeded srand(time(0)
Reply
Posted 2004-11-07, 01:14 PM
I know how to make a bubble sort and the exchange sort but I do not know how to generate a small arrays of dimension 20, such that the entries are random positive integers less than 100. I am also not sure if I can combine the bubble, exchange and sort the random array of integers. Heres what I have so far:

Bubble Sort:
Code:
#include <stdio.h>

main()
{
 int numbers[10], i, j, k, tmp;
 printf("\nthe original numbers are:\n");
 for (i=0; i<10; i=i+1)
 {
  numbers[i]=rand();
  printf(" %d ", numbers[i]);
 }
 printf("\n\n");
 for (i=0; i<10; i=i+1)
 {
   for (j=0; j<9; j=j+1)
   {
     if (numbers[j]<numbers[j+1])
     {
       tmp=numbers[j];
       numbers[j]=numbers[j+1]
       numbers[j+1]=tmp;
     }
   }
  }
   printf("the sorted numbers are:\n")
   for (i=0; i<10; i=i+1)
   {
    printf(" %d ", numbers[i]);
   }
 printf("\n\n");
}

Exchange Sort:
#include <stdio.h>

main()
{
 int numbers[10], i, j, k, tmp;
 printf("\nthe original numbers are:\n");
 for (i=0; i<10; i=i+1)
 {
  numbers[i]=rand();
  printf(" %d ", numbers[i]);
 }
 printf("\n\n");
 for (i=0; i<9; i=i+1)
 {
   for (j=0; j<10; j=j+1)
   {
     if (numbers[i]<numbers[j])
     {
       tmp=numbers[j];
       numbers[j]=numbers[i]
       numbers[i]=tmp;
     }
   }
  }
   printf("the sorted numbers are:\n")
   for (i=0; i<10; i=i+1)
    printf(" %d ", numbers[i]);
 printf("\n\n");
return(0);
}

Last edited by WetWired; 2004-11-08 at 07:01 AM. Reason: Please use the [code] tag
Old
Profile PM WWW Search
deadlock75 is neither ape nor machine; has so far settled for the in-betweendeadlock75 is neither ape nor machine; has so far settled for the in-between
 
deadlock75