sortarea
DESCRIPTION
.TRANSCRIPT
![Page 1: sortarea](https://reader036.vdocuments.site/reader036/viewer/2022083007/563dbafc550346aa9aa94b99/html5/thumbnails/1.jpg)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int comp_shaker,comp_comb;
int perm_shaker,perm_comb;
void shaker(int *items, int count)
{
register int a;
int exchange;
char t;
do {
exchange = 0;
for(a=count-1; a > 0; --a) {
if(items[a-1] > items[a]) {
comp_shaker++;
t = items[a-1];
items[a-1] = items[a];
perm_shaker++;
items[a] = t;
perm_shaker++;
exchange = 1;
}
}
for(a=1; a < count; ++a) {
if(items[a-1] > items[a]) {
comp_shaker++;
t = items[a-1];
items[a-1] = items[a];
perm_shaker++;
items[a] = t;
perm_shaker++;
exchange = 1; }
![Page 2: sortarea](https://reader036.vdocuments.site/reader036/viewer/2022083007/563dbafc550346aa9aa94b99/html5/thumbnails/2.jpg)
}
} while(exchange);
}
void combsort(int a[], int nElements)
{
int i, j, gap, swapped = 1;
double temp;
gap = nElements;
while (gap > 1 || swapped == 1)
{
gap = gap * 10 / 13;
if (gap == 9 || gap == 10) gap = 11;
if (gap < 1) gap = 1;
comp_comb++;
perm_comb++;
swapped = 0;
for (i = 0, j = gap; j < nElements; i++, j++)
{
if (a[i] > a[j])
{
comp_comb++;
temp = a[i];
perm_comb++;
a[i] = a[j];
perm_comb++;
a[j] = temp;
swapped = 1;
}
}
}
}
![Page 3: sortarea](https://reader036.vdocuments.site/reader036/viewer/2022083007/563dbafc550346aa9aa94b99/html5/thumbnails/3.jpg)
int main(void)
{
int a[100];
int b[100];
int n;
printf("introduceti numarulde cifre:");
scanf("%d",&n);
printf("introduceti numerele:");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
printf(" \n Metoda shaker \n ");
shaker(a,n);
for(int i=0;i<n;i++)
{
printf("%d,",a[i]);
}
printf("\n\n Numarul de comparatii:%d",comp_shaker);
printf("\n Numarul de permutari:%d",perm_shaker);
printf("\n \n");
printf("\n Metoda sortarii prin interschimb(combsort) \n ");
combsort(b,n);
for(int i=0;i<n;i++)
{
printf("%d,",b[i]);
}
printf("\n\n Numarul de comparatii:%d",comp_comb);
printf("\n Numarul de permutari:%d",perm_comb);
printf("\n \n");
return 0; }