Exemplo n.º 1
0
/* *** ODSATag: MergesortOpt *** */
void mergesortOpt(Comparable* A[], Comparable* temp[], int left, int right) {
  int i, j, k, mid = (left+right)/2;// Select the midpoint
  if (left == right) return;          // List has one record
  if ((mid-left) >= THRESHOLD) mergesortOpt(A, temp, left, mid);
  else inssort(A, left, mid);
  if ((right-mid) > THRESHOLD) mergesortOpt(A, temp, mid+1, right);
  else inssort(A, mid+1, right);
  // Do the merge operation.  First, copy 2 halves to temp.
  for (i=left; i<=mid; i++) *temp[i] = *A[i];
  for (j=right; j>mid; j--) *temp[i++] = *A[j];
  // Merge sublists back to array
  for (i=left,j=right,k=left; k<=right; k++)
    if (*temp[i] <= *temp[j]) *A[k] = *temp[i++];
    else *A[k] = *temp[j--];
}
Exemplo n.º 2
0
int main(int argc, char** argv) {
    Exec_Name = basename(argv[0]);
    int count = 0;
    double array[1000];
    size_t length;
    if(argc != 1){
        fprintf (stderr, "%s has no condition.\n",
                  Exec_Name);
        Exit_Status = EXIT_FAILURE;   
    }
    for(int i=0; i<1000; i++){ 
        double input;
        int scanct = scanf("%lf", &input);
        if(scanct == EOF) {
            break;
        }
        if(scanct != 1){
            fprintf (stderr, "invalid input.\n");
            Exit_Status = EXIT_FAILURE;
            break;
        }
        array[i]=input;   
        count++;
        length = count;
    }
    inssort(array, count, sizeof(double), compare);
    for(int j=0; j<count; j++){
        printf("%20.15g\n", array[j]);
    }
    return (Exit_Status);
}
Exemplo n.º 3
0
Arquivo: sort.c Projeto: ankitpati/fds
int main()
{
    unsigned n, ch;
    int a[50];
    n=0;
    do{
        puts("What would you like to do?");
        puts(" ( 0) Exit");
        puts(" ( 1) Populate Array");
        puts(" ( 2) Print Array");
        puts(" ( 3) Bubble Sort");
        puts(" ( 4) Insertion Sort");
        puts(" ( 5) Selection Sort");
        puts(" ( 6) Quick Sort");
        puts(" ( 7) Merge Sort");
        scanf(" %u%*c", &ch);
        switch(ch){
        case 0:
            puts("Bye!");
            break;
        case 1:
            accept(a, &n);
            break;
        case 2:
            display(a, n);
            break;
        case 3:
            bubsort(a, n);
            display(a, n);
            break;
        case 4:
            inssort(a, n);
            display(a, n);
            break;
        case 5:
            selsort(a, n);
            display(a, n);
            break;
        case 6:
            qiksort(a, n);
            display(a, n);
            break;
        case 7:
            mrgsort(a, n);
            display(a, n);
            break;
        default:
            puts("Incorrect Choice!");
            break;
        }
        putchar('\n');
    } while(ch);
    putchar('\n');
    return 0;
}
Exemplo n.º 4
0
void sort(int *array, int* temp, int i, int j)
{
	if(j-i < SORTHRESH) inssort(array, temp, i, j);
	else
	{
		int pivotindex = findpivot(i, j);
		swap(array+pivotindex, array+j);
		swap(temp+pivotindex, temp+j);
		int k = partition(array, temp, i-1, j, array[j]);
		swap(array+k, array+j);
		swap(temp+k, temp+j);
		if((k-i)>1) sort(array, temp, i, k-1);
		if((j-k)>1) sort(array, temp, k+1, j);
	}
}
Exemplo n.º 5
0
void ssort2(unsigned char **a, int n, int depth)
{   int d, r, partval;
    unsigned char **pa, **pb, **pc, **pd, **pl, **pm, **pn, *t;
    if (n < 10) {
        inssort(a, n, depth);
        return;
    }
    pl = a;
    pm = a + (n/2);
    pn = a + (n-1);
    if (n > 30) { 
        d = (n/8);
        pl = med3(pl, pl+d, pl+2*d);
        pm = med3(pm-d, pm, pm+d);
        pn = med3(pn-2*d, pn-d, pn);
    }
    pm = med3(pl, pm, pn);
    swap2(a, pm);
    partval = ptr2char(a);
    pa = pb = a + 1;
    pc = pd = a + n-1;
    for (;;) {
        while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) {
            if (r == 0) { swap2(pa, pb); pa++; }
            pb++;
        }
        while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) {
            if (r == 0) { swap2(pc, pd); pd--; }
            pc--;
        }
        if (pb > pc) break;
        swap2(pb, pc);
        pb++;
        pc--;
    }
    pn = a + n;
    r = min(pa-a, pb-pa);    vecswap2(a,  pb-r, r);
    r = min(pd-pc, pn-pd-1); vecswap2(pb, pn-r, r);
    if ((r = pb-pa) > 1)
        ssort2(a, r, depth);
    if (ptr2char(a + r) != 0)
        ssort2(a + r, pa-a + pn-pd-1, depth+1);
    if ((r = pd-pc) > 1)
        ssort2(a + n-r, r, depth);
}
Exemplo n.º 6
0
int main (int argc, char* argv[]) {
    //Creation of array
    double array[0x1000] = {};
    size_t in_count = 0;
    //Populate the array: stop at end of array, EOF, or invalid input
    for (size_t index = 0; index < 0x1000; index++) {
        int in_status = scanf ("%lg", &array[index]);
        if (in_status <= 0) break;
        ++in_count;
    }
    //Pass the array to be sorted, pass input count
    inssort (array, in_count, sizeof (*array), d_cmp);
    //Print the array
    for (size_t index = 0; index < in_count; index++) {
        printf ("%20.15g\n", array[index]);
    }
    return (0);
}
Exemplo n.º 7
0
void main()
{
	int i, s, a[20];
	clrscr();
	printf("Enter no. of elements: ");
	scanf("%d", &s);
	for(i=0; i<s; i++)
	{
		printf("Enter element %d: ", i+1);
		scanf("%d", &a[i]);
	}
	inssort(a, s);
	for(i=0; i<s; i++)
	{
		printf("%d\n", a[i]);
	}
	getch();
}
Exemplo n.º 8
0
int main()
{
    int T,N,ITEM,AR[100000],i;
    T<=5;
    AR[i]<=1000000;
    printf("how many test cases u want to enter?");
    scanf("%d",&T);
    for(int m=0;m<T;m++)
    {
       printf( "\nhow many elements do u want to enter?");
      scanf("%d",&N);
         printf("\n enter array elements");
        for(i=1;i<=N;i++)
           scanf("%d",&AR[i]) ;
        inssort(AR,N);
    }
    getch();
}
Exemplo n.º 9
0
int main(void) {
    int a[N];

    std::cout << "Before:";
	for (int i = 0; i < N; i++) {
		a[i] = rand() / (RAND_MAX / 100 + 1);
		std::cout << " " << a[i];
	}

	std::cout << std::endl;
	inssort(N, a);

    std::cout <<"After: ";
	for (int i = 0; i < N; i++) {
		std::cout << " " << a[i];
	}
	
    return EXIT_SUCCESS;
}
Exemplo n.º 10
0
void backtrace(int t,int x,int y)
{
	if(t>=M*N)
	{
		int i,j;
		for(i=0;i<8;i++)
		{
			if((x+jump[i][0])==0 && (y+jump[i][1])==0)
			{
				output();
				exit(1);
			}
		}
	}
	else
	{
		int i;
		int count[8],possiblesteps[8];
		int k=0;
		for(i=0;i<8;i++)
		{
			if(canjump(x+jump[i][0],y+jump[i][1]))
			{
				count[k]=weightstep(x+jump[i][0],y+jump[i][1]);
				possiblesteps[k++]=i;
			}
		}
		inssort(count, possiblesteps,k);
		for(i=0;i<k;i++)
		{
			int d=possiblesteps[i];
			x+=jump[d][0];
			y+=jump[d][1];
			chess[x][y]=t+1;
			//step[t]=d;
			backtrace(t+1,x,y);
			chess[x][y]=0;
			x-=jump[d][0];
			y-=jump[d][1];
		}
	}
}
Exemplo n.º 11
0
int main (int argc, char* argv[]) {
    //Creation of array
    char* array[0x1000] = {};
    char buffer[0x1000] = {};
    int in_count = 0;
    //Populate the array: stop at end of array, EOF, or invalid input
    for (size_t index = 0; index < 0x1000; index++) {
        char* in_string = fgets (buffer, 0x1000, stdin);
        if (in_string == NULL) break;
        array[index] = strdup (in_string);
        ++in_count;
    }
    //Pass the array to be sorted, pass input count
    inssort (array, in_count, sizeof (*array), s_cmp);
    //Print the array
    for (size_t index = 0; index < in_count; index++) {
        printf ("%s", array[index]);
    }
    return (0);
}
Exemplo n.º 12
0
/*Main---------------------------------------------------------------*/
int main() {

  //array declarations
  double array[ARRAY_SIZE];

  //loop through storing to array
  int i;
  for (i = 0; scanf("%lf", &array[i]) != EOF && i < ARRAY_SIZE - 1;
          i++);

  //Make constant variable of the size of input
  const size_t nelem = i;

  //insert sorted
  inssort(array, nelem, sizeof *array, floatcmp);

  //print array
  printarray(array, nelem);

  //return success
  return EXIT_SUCCESS;
}
int main (int argc, char **argv) {
    char* buffer[1000];
    for(int i = 0; i > 1000; i++) {
        buffer[i] = NULL;
    }

    int count = 0;
    for(;;) {
        char *temp = malloc(sizeof(char*));
        int scanct = scanf("%s",temp);
        if (feof(stdin)) {
            printf ("EOF\n");
            break;
        }
        if(count >= 1000) {
            break;
        }
        buffer[count] = strdup(temp);
        if(buffer[count] == NULL) {
            break;
        }
        count++;
        free(temp);
    }
    inssort(buffer,count,sizeof (double *),charcmp);
    for(int i = 0; i < 1000; i++) {
        if(buffer[i] == NULL) {
            break;
        }
        // printf("write sum'in");
        printf("%s\n",buffer[i]);
    }


    return 0;
}
Exemplo n.º 14
0
int main (){
    char* array[1000];
    char buffer[1000];
    int max = 0;
    for (int i = 0;i < 999; ++i){
       cstring line;
       line = fgets (buffer, sizeof buffer, stdin);
       if (line == NULL) break;
       cstring nlpos = strchr (buffer, '\n');
       if (nlpos != NULL){
          *nlpos = '\0';
       }

       array [i] = strdup(buffer);
       ++max;
    }

    inssort (&array[0], max, sizeof(char *) ,compar);

    for (int i = 0; i < max; i++){
       printf ("%s\n", array[i]);
       free (array[i]);
    }
}