Exemplo n.º 1
0
Key* PerformBSort(Key* head)
{
	Key* ckey = head;
	while (ckey)
	{
		ckey->setValuePtr(BubbleSort2(ckey->getValuePtr()));
		//Go to next key (if any)
		ckey = ckey->getNext();
	}
	return BubbleSort2(head);
}
Exemplo n.º 2
0
int main()
{
  int array[] = {10,9,8,7,6,5,4,3,2,1};
  int len = sizeof(array) / sizeof(int);
  PrintArray(array,len,"冒泡排序前:");
  //BubbleSort(array,len);
  BubbleSort2(array,len);
  PrintArray(array,len,"冒泡排序后:");
  return 0;
}
Exemplo n.º 3
0
int main(void)
{
    int i;
    u64 begin,end;
    ElemType* const array = (int*)malloc(sizeof(int)*ARRAY_NUMBER );
    InitalArray(array);
    PrintArray(array);

    ElemType* tempArray1 =  (int*)malloc(sizeof(int)*ARRAY_NUMBER );
    ElemType* tempArray2 =  (int*)malloc(sizeof(int)*ARRAY_NUMBER );
    ElemType* tempArray3 =  (int*)malloc(sizeof(int)*ARRAY_NUMBER );
    for(i = 0; i < ARRAY_NUMBER ; i++)
    {
        tempArray1[i] = array[i];
        tempArray2[i] = array[i];
        tempArray3[i] = array[i];
    }

    //冒泡排序
    Rdtsc(&begin);
    BubbleSort(array);
    Rdtsc(&end);
    printf("冒泡排序后的数组是:");
    PrintArray(array);
    printf("花费时间为:%llu \n",end - begin);

    Rdtsc(&begin);
    BubbleSort2(tempArray1);
    Rdtsc(&end);
    printf("冒泡排序2后的数组是:");
    PrintArray(tempArray1);
    printf("花费时间为:%llu \n",end - begin);

    Rdtsc(&begin);
    BubbleSort3(tempArray2);
    Rdtsc(&end);
    printf("冒泡排序3后的数组是:");
    PrintArray(tempArray2);
    printf("花费时间为:%llu \n",end - begin);
    
    Rdtsc(&begin);
    BubbleSort4(tempArray3);
    Rdtsc(&end);
    printf("冒泡排序4后的数组是:");
    PrintArray(tempArray3);
    printf("花费时间为:%llu \n",end - begin);

    return 0;
}
Exemplo n.º 4
0
int main(int argc, const char* argv[])
{
  int i;
  SqList *L;
  L = (SqList *) malloc(sizeof(SqList));
  L->r[0] = 0;
  for (int i = 1; i < 11; i++)
    L->r[i] = 11 - i;
  L->length = 11;
  BubbleSort2(L);
  for (int i =1; i < 11; i++)
    printf("%d ", L->r[i]);
  puts("");
  return 0;
}
Exemplo n.º 5
0
int main(int argc, char * argv[]) {
    int numOfLists, sizeOfLists, i, j, bufferItem;
    double bubBest1, bubWorst1, bubAvg1, bubBest2, bubWorst2, bubAvg2, bubBest3, bubWorst3, bubAvg3, sum1, sum2, sum3;
    List Lists[1000];
    List bufferList;
    clock_t start1,end1, start2, end2, start3, end3;
    srand(time(NULL));
    if ((argc < 3) || (argc > 3)) { /*Checks preconditions for program*/
        printf("**Invalid number of arguments to run program**\n\n**Program will terminate now\n");
        return 1;
    }
    numOfLists = atoi(argv[1]);
    sizeOfLists = atoi(argv[2]); /*only lists of size 1000 and up to 1000 lists are supported*/
    if ((numOfLists>1000) || (numOfLists<=2) || (sizeOfLists>1000) || (sizeOfLists<=2)) {
        printf("**Invalid integer entered**\nOnly integers between 3 and 1000 are allowed\nProgram will terminate now\n");
        return 1;
    }
    Initialize(&bufferList);
    for (i=0; i<sizeOfLists; i++) {  /*Create two mandatory Lists*/
        Insert(i+1, i, &Lists[0]);
        Insert(sizeOfLists-i, i, &Lists[1]);
    }
    for (i=2; i<numOfLists; i++) {
        for (j=0; j<sizeOfLists; j++) {
            bufferItem = (rand() %343+1);  /*Create random lists*/
            Insert(bufferItem, bufferList.size, &bufferList);
        }
        Lists[i] = bufferList;
        Initialize(&bufferList);
    }
    sum1=0;
    bubAvg1 = 0;
    start1 = clock();         /*Starts Timer*/
    BubbleSort1(Lists[0]);
    end1 = clock();
    bubBest1 = (double)(end1-start1)/CLOCKS_PER_SEC;

    start2 = clock();
    BubbleSort1(Lists[1]);
    end2 = clock();
    bubWorst1 = (double)(end2-start2)/CLOCKS_PER_SEC;

    for (i=0; i<numOfLists; i++) {
        start3 = clock();
        BubbleSort1(Lists[i]);
        end3 = clock();
        sum1 = ((double)(end3-start3)/CLOCKS_PER_SEC);
        bubAvg1 = bubAvg1 + sum1;                  /*Finds the Average*/
    }
    bubAvg1 = bubAvg1/numOfLists;

    printf("\nBubbleSort1\n%f\n%f\n%f\n", bubBest1, bubAvg1, bubWorst1);

    sum2=0;
    bubAvg2 = 0;
    start1 = clock();
    BubbleSort2(Lists[0]);
    end1 = clock();
    bubBest2 = (double)(end1-start1)/CLOCKS_PER_SEC;

    start2 = clock();
    BubbleSort2(Lists[1]);
    end2 = clock();
    bubWorst2 = (double)(end2-start2)/CLOCKS_PER_SEC;

    for (i=0; i<numOfLists; i++) {
        start3 = clock();
        BubbleSort2(Lists[i]);
        end3 = clock();
        sum2 = ((double)(end3-start3)/CLOCKS_PER_SEC);
        bubAvg2 = bubAvg2 + sum2;
    }
    bubAvg2 = bubAvg2/numOfLists;


    printf("\nBubbleSort2\n%f\n%f\n%f\n", bubBest2, bubAvg2, bubWorst2);

    sum3=0;
    bubAvg3 = 0;
    start1 = clock();
    MergeSort(&Lists[0], 0, Lists[0].size-1);
    end1 = clock();
    bubBest3 = (double)(end1-start1)/CLOCKS_PER_SEC;

    start2 = clock();
    MergeSort(&Lists[1], 0, Lists[1].size-1);
    end2 = clock();
    bubWorst3 = (double)(end2-start2)/CLOCKS_PER_SEC;

    for (i=0; i<numOfLists; i++) {
        start3 = clock();
        MergeSort(&Lists[i], 0, Lists[i].size-1);
        end3 = clock();
        sum3 = ((double)(end3-start3)/CLOCKS_PER_SEC);
        bubAvg3 = bubAvg3 + sum3;
    }
    bubAvg3 = bubAvg3/numOfLists;

    printf("\nMergeSort\n%f\n%f\n%f\n", bubBest3, bubAvg3, bubWorst3);

    return EXIT_SUCCESS;
}