Dinamik Hafıza İşgali Yöntemi Girilen Sayı Dizinindeki Sayıları Artan Veya Azalan Olarak Sıralama

mustaphos

MB Üyesi
Kayıt
14 Eylül 2015
Mesajlar
35
Tepkiler
8
Yaş
29
Meslek
Öğrenci
Üniv
Anadolu University
Merhabalar.
Program önce kaç adet değer girileceğini ve değerleri alır. Sonra istenen düzene göre sıralar.
İyi çalışmalar.

#include <stdio.h>// Libraries
#include <stdlib.h>// Libraries

float *sirala (float *yeri, int kactane, int tip,float *dizinim);// Function prototype in pointer type

void main()
{
int kactane;// Variable defines
int i ;// Variable defines
int tip;// Variable defines
float *yeri;// Variable defines
float *dizinim;// Variable defines
float *yazdir;// Variable defines

printf("Please write how many number you want to sort\n");
printf(">>> ");
scanf("%d",&kactane);// Get how much number you want to sort from user

dizinim = (float*)malloc(kactane*sizeof(float));// Define the size of array
yeri=dizinim;// Get the array's starting address

i=0;
do
{
printf("%d.th element >>> ",i);
scanf("%f",&dizinim);// Get the floating points from user
i++;
}
while(i<kactane);

printf("Please write which sort type you want to apply on the array\n");
printf("[0] for descending order\n");
printf("[1] for ascending order\n");
printf(">>> ");
scanf("%d",&tip);// Get which sort type the user wants to sort the array

yazdir = sirala(yeri,kactane,tip,dizinim);// Define a variable that calls the function

i=0;
do
{
printf("%d.th element >>> ",i);
printf("%0.3f \n",*yazdir++);// Variable points the elements of array and increases itself
i++;
}
while(i<kactane);

getch();
}

float *sirala (float *yeri, int kactane, int tip,float *dizinim)// Sort function in pointer type
{
int i;// Variable defines
int sayac;// Variable defines
static float *yenidizinim;// Variable defines
float gecici;// Variable defines

yenidizinim = (float*)malloc(kactane*sizeof(float));// Define the size of array

i=0;
do
{
yenidizinim=dizinim;// Equalize the static array's and array's elements to each other
i++;
}
while(i<kactane);

i=0;
do
{

sayac=0;
do
{

switch (tip)// Swtich case for sorting type
{

case 1 :// If sort type is ascending
{
if(yenidizinim[sayac]>yenidizinim[sayac+1])// If n.th element greater than n+1.th element then switch each other
{
gecici=yenidizinim[sayac+1];
yenidizinim[sayac+1]=yenidizinim[sayac];
yenidizinim[sayac]=gecici;
}
break;
}

case 0 :// If sort type is descending
{
if(yenidizinim[sayac]<yenidizinim[sayac+1])// If n.th element less than n+1.th element then switch each other
{
gecici=yenidizinim[sayac+1];
yenidizinim[sayac+1]=yenidizinim[sayac];
yenidizinim[sayac]=gecici;
}
break;
}

}

sayac++;
}
while(sayac<kactane-1);// Because if we sort n-1.th element, the n.th element also be sorted

i++;
}
while(i<kactane);

return yenidizinim;
}
 
Yukarı Alt