Selection Sort örneği

Hits: 9

image_pdfpdf indir
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
 
#define MAX_ITEMS 100
 
void main(void);
void selection(double x[], int n);
 
void main(void)
{
   double x[MAX_ITEMS];
   int num_items;
   int i;
 
   printf("\nSELECTION SIRALAMA ALGORITMASI");
   printf("\nSiralanac sayi adedi (Max=100) : ");
   scanf("%d",&num_items);
 
   for (i = 0; i < num_items; i++)
   {
    printf("\n%d. sayi : ",i+1);
         scanf("%lf",&x[i]);
   }
 
   //CALL SELECTION TYPE SORTING FUNCTION
   selection(x,num_items);
 
   //PRINT SORTED LIST
   printf("\nSayilarin Siralanmis hali : ");
   for (i = 0; i < num_items; i++)
    printf(" %.lf ",x[i]);
 
   getch();
}
 
void selection(double x[], int n)
{
   int pass;
   int i;
   int imin=0;
   double min;
   double temp;
 
   for (pass = 0; pass < n - 1; ++pass)
   {
      min  = x[pass];
      imin = pass;
 
      for(i = pass+1; i < n; ++i)
      {
        if (x[i] < min)
         {
               min = x[i];
            imin = i;
         }
      }
 
      temp = x[pass];
      x[pass] = x[imin];
      x[imin] = temp;
   }
}