Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by Mobyman ( 17 years ago )
#include <stdio.h>
#include <stdlib.h>
#define COCKTAIL 1
#define MERGE 2
#define NORMAL 1
#define RANDOM 2
#define INVERT 3
#define NEARLY 4
int cocktail(int *mass, int size, int first, int last){
int i;
for (;first<last;last--,first++)
{
for (i=first;i<last;i++)
{
if (mass+i>mass+i+1)
swap(&mass+i, &mass+i+1);
}
for (i=last;i>first;i--)
{
if (mass+i<mass+i-1)
swap(&mass+i, &mass+i-1);
}
}
return 0;
}
int swap(int **a, int **b)
{
int temp = **a;
**a = **b;
**b = temp;
return 0;
}
int randomfill(int *mass, int size){
int i;
for (i=0;i<=size;i++)
*(mass+i) = rand()%(size*10);
return 0;
}
int reversefill(int *mass, int size){
int i;
for (i=0;size>=0;i++){
*(mass+i) = size;
size--;
}
return 0;
}
int normalfill(int *mass, int size){
int i;
for(i=1;i<size;i++)
*(mass+i) = i;
return 0;
}
int swapfill(int *mass,int size){
int i;
for (i=0;i<=size;i+=2){
swap(&mass+i,&mass+i+1);
}
return (int)*mass;
}
int merge(int **a, long lb, long split, long ub)
{
long pos1 = lb;
long pos2 = split+1;
long pos3 = 0;
int *temp = (int*)malloc((ub - lb + 1) * sizeof(int));
while (pos1 <= split && pos2 <= ub)
{
if (*a+pos1 < *a+pos2)
temp[pos3++] = **a+pos1++;
else
temp[pos3++] = **a+pos2++;
}
while (pos2 <= ub)
temp[pos3++] = **a+pos2++;
while (pos1 <= split)
temp[pos3++] = **a+pos1++;
for (pos3 = 0; pos3 < ub-lb+1; pos3++)
**(a+lb+pos3) = temp[pos3];
free(temp);
}
void mergeSort(int *a, long lb, long ub)
{
long split;
if (lb < ub)
{
split = (lb + ub)/2;
mergeSort(a, lb, split);
mergeSort(a, split+1, ub);
merge(&a, lb, split, ub);
}
}
int main(int argc, char *argv[])
{
if (argc==4) {
int size=*argv[2], method=0, status=0, mass[size], i;
switch(*argv[3]){
case 1:
status = NORMAL;
normalfill(&mass, size);
break;
case 2:
randomfill(&mass, size);
status = RANDOM;
break;
case 3:
reversefill(&mass, size);
status = INVERT;
break;
case 4:
swapfill(&mass, size);
status = NEARLY;
break;
default:
return 1;
break;
}
switch(*argv[1]){
case 1:
cocktail (&mass,size,0,size+1);
for (i = 1; i < size+1; i++){
printf ("%d
", mass[i]);}
method = COCKTAIL;
break;
case 2:
mergeSort(mass, 0, size);
for (i = 1; i < size+1; i++){
printf ("%d
", mass[i]);
method = MERGE;
break;
default:
return 1;
break;
}
for (i = 1; i < size+1; i++){
printf ("%d
", mass[i]);
}
return 0;
}} else
printf("Введите параметры запуÑка!
./sort <int вид_Ñортировки> <количеÑтво Ñлементов> <ÑоÑтоÑние до Ñортировки>
Виды Ñортировки
cocktail(1) || merge(2)
УпорÑдоченноÑть до Ñортировки
normal(1), random(2), reverse(3), nearly(4)
Пример: ./sort 2 10 3
");
return 1;
}
Revise this Paste
Parent: 6561