Пример #1
0
int main()	//this program is used to test the speed of bubbleSort , selectSort , shellSort , insertSort
{
int a[100]={-100,10,4,2,342,2111,0,-2,31121,3131,1,212,12,313,423,54,8658,4,54,32,-223,424,-535,-555,-33,34,5,3,0,33,5332,-3333333,333333,434,532225,5600,33232,53333,-3323,0,0,22332,3235532,44,45,46,47,48,49,50,4142,3213,21213,12,2424,532,-343,31232,1233214,421245,3232432,1231312,42,0,42,4222,3,-424242424,4222,78900,776,72,73,74,454,64343,2234,113,1244,242,4333553,23432,83,49902,3323,-2324,-42442,-323,89,90,3333,323,44,2,2222,323,97,98,99,100};
int b[100]={-100,10,4,2,342,2111,0,-2,31121,3131,1,212,12,313,423,54,8658,4,54,32,-223,424,-535,-555,-33,34,5,3,0,33,5332,-3333333,333333,434,532225,5600,33232,53333,-3323,0,0,22332,3235532,44,45,46,47,48,49,50,4142,3213,21213,12,2424,532,-343,31232,1233214,421245,3232432,1231312,42,0,42,4222,3,-424242424,4222,78900,776,72,73,74,454,64343,2234,113,1244,242,4333553,23432,83,49902,3323,-2324,-42442,-323,89,90,3333,323,44,2,2222,323,97,98,99,100};
int c[100]={-100,10,4,2,342,2111,0,-2,31121,3131,1,212,12,313,423,54,8658,4,54,32,-223,424,-535,-555,-33,34,5,3,0,33,5332,-3333333,333333,434,532225,5600,33232,53333,-3323,0,0,22332,3235532,44,45,46,47,48,49,50,4142,3213,21213,12,2424,532,-343,31232,1233214,421245,3232432,1231312,42,0,42,4222,3,-424242424,4222,78900,776,72,73,74,454,64343,2234,113,1244,242,4333553,23432,83,49902,3323,-2324,-42442,-323,89,90,3333,323,44,2,2222,323,97,98,99,100};
int d[100]={-100,10,4,2,342,2111,0,-2,31121,3131,1,212,12,313,423,54,8658,4,54,32,-223,424,-535,-555,-33,34,5,3,0,33,5332,-3333333,333333,434,532225,5600,33232,53333,-3323,0,0,22332,3235532,44,45,46,47,48,49,50,4142,3213,21213,12,2424,532,-343,31232,1233214,421245,3232432,1231312,42,0,42,4222,3,-424242424,4222,78900,776,72,73,74,454,64343,2234,113,1244,242,4333553,23432,83,49902,3323,-2324,-42442,-323,89,90,3333,323,44,2,2222,323,97,98,99,100};

double start,end;int i;
start=(double)clock();
bubbleSort(a,100);
end=(double)clock();
printf("bubbleSort time is %.2f\n",(end-start)/1000);

start=(double)clock();
selectSort(b,100);
end=(double)clock();
printf("selectSort time is %.2f\n",(end-start)/1000);

start=(double)clock();
shellSort(c,100);
end=(double)clock();
printf("shellSort time is %.2f\n",(end-start)/1000);

start=(double)clock();
insertSort(d,100);
end=(double)clock();
printf("insertSort time is %.2f\n",(end-start)/1000);

start=(double)clock();
quickSort(d,100);
end=(double)clock();
printf("quickSort time is %.2f\n",(end-start)/1000);

}
Пример #2
0
int main(void)
{
    std::vector<int> rdata = random_int(15);
    print(rdata, std::cout, '=', "Original arr");

    std::vector<int> trdata = rdata;
    bubbleSort(trdata);
    print(trdata, std::cout, '=', "bubble sort");

    trdata = rdata;
    insertSort(trdata);
    print(trdata, std::cout, '=', "insert sort");

    trdata = rdata;
    selectSort(trdata);
    print(trdata, std::cout, '=', "select sort");

    trdata = rdata;
    mergeSort(trdata);
    print(trdata, std::cout, '=', "merge sort");

    trdata = rdata;
    quickSort(trdata);
    print(trdata, std::cout, '=', "quick sort");

    return 0;
}
Пример #3
0
void superQuickSort(int a[], int left, int right)//quick-sort дл¤ менее чем 10 эл-тов использует сортировку вставками
{
	if (right - left < 10)
		insertSort(a, left, right);
	else
	{
		int i = left;
		int j = right;
		int base = a[(left + right) / 2];
		while (i <= j)
		{
			while (a[i] < base)
				i++;
			while (a[j] > base)
				j--;
			if (i <= j)
			{
				int tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
				i++;
				j--;
			}
		}
	
	
		if (left < j)
			superQuickSort(a, left, j);
		if (i < right)
			superQuickSort(a, i, right);
	}
}
Пример #4
0
int
main( int argc, char** argv )
{

    long *_list     = NULL;
    int  _i         = 0;
    char *_endptr   = NULL;

    if( argc < 2 )
    {
        usage();
        exit(EXIT_FAILURE);
    }

    _list = (long *)calloc( argc, sizeof(long) );
    if( NULL == _list )
    {
        printf("メモリ獲得エラー\n");
        exit(EXIT_FAILURE);
    }

    // 数値へ変換
    for( _i = 0; _i < argc - 1; _i++ )
    {
        _list[_i+1] = strtol( argv[_i+1], &_endptr, BASE );
    }

    printList( _list, argc - 1 );

    insertSort( _list, argc );

    printList( _list, argc - 1 );

    return 0;
}
void quickSort(int s[], int left, int right)
{
	int i,j;
	int centerValue=0;

	if(right-left>2)		//more than 3 element
	{

		centerValue=media(s,left,right);
		i=left;
		j=right-2;
		while(1)
		{
			while(s[i]<centerValue){i++;}
			while(s[j]>centerValue){j--;}
			if(i>j)
				break;
			else
				swap(&s[i],&s[j]);
		}
		//recover the right-1
		swap(&s[right-1],&s[i]);
		quickSort(s,left,i-1);
		quickSort(s,i+1,right);

	}
	else					//less than 3 element using insertSort, bubbleSort or others sort algorithms
							//based on swap, count sort is an exception.
	{
		insertSort(s,left,right);
	}

	return;
}
Пример #6
0
void qSort(ElementType A[], int left, int right)
{
    int i, j;
    ElementType pivot;
    if(left + CUTOFF <= right)
    {
        pivot = median3(A, left, right);
        i = left;
        j = right - 1;
        while(1)
        {
            while(A[++i] < pivot) { }
            while(A[--j] > pivot) { }
            if(i < j)
                swap(&A[i], &A[j]);
            else
                break;
        }
        swap(&A[i], &A[right - 1]); //restore pivot
        
        qSort(A, left, i-1);
        qSort(A, i+1, right);
    }
    else
        insertSort(A+left, right-left+1);
}
Пример #7
0
int main() {

	int num[] = {12, 1, 34, 33, 56, 7};
	insertSort(num, 6);
	print(num, 6);
	return 0;
}
Пример #8
0
int main(int argc, char *argv[]) {
    if (argc<2) return 0;
    FILE *f = fopen(argv[1],"r");
    if (f==NULL) return 0;
    struct List *l;
    newList(&l);
    char x[20];
    struct Entry* node;
    while (fscanf(f,"%s",x)==1) {
        if (x[0] != '"') {
            //newNode(atoi(x),&node);
            insertSort(&l,atoi(x));
        }
    }
    fclose(f);
    reverse(&l);
    node = l->head;
    while (node->next != l->head) {
        printf("%d ",node->next->element);
        node = node->next;
    }
    printf(" %d",l->size);
    return 0;


}
Пример #9
0
int main(int argc, const char * argv[])
{

    //printList(appendNode(&(BuildWithLocalRef()), 2)); //doesn't work? &(2376ut) doesn't work not a lvalue
    Node* head=BuildOneTwoThree();
    appendNodeWithPush(&head, 4);
    appendNode(&head, 4);
    appendNode(&head, 4);
    Node* test5=BuildWithLocalRef();
    Push(&test5, 6);
    insertNth(&test5, 4, 3);
    //printList(test5);
    insertSort(&test5);
    append(&test5, &head);
    //printList(test5);
    Node *first=NULL,*second=NULL;
    //pop(&test5);
    //split(test5, &first, &second);
    //printList(first);
    //printList(second);
    //removeDuplicates(second);
    //printList(first);
    //printList(second);
    //moveNode(&first, &second);
    //printList(first);
    //printList(second);
    alternatingSplit(test5, &first, &second);
    //printList(first);
    //printList(second);
    //Node* test=shuffleMergeRecursive(first, second);
    //printList(test);
    Node* test1=BuildOneTwoThree();
    Node* test2=BuildOneTwoThree();
    pop(&test2);
    appendNode(&test2, 7);;
    appendNode(&test1, 5);
    //printList(test1);
    //printList(test2);
    append(&test1, &test2);
    //printList(test1);
    mergeSort(&test1);
    //printList(test1);
    Node* t4=BuildOneTwoThree();
    Node* t5=BuildOneTwoThree();
    appendNode(&t5, 6);
    appendNode(&t4, 6);
    appendNode(&t4, 7);
    appendNode(&t5, 8);
    Push(&t5, 0);
    Push(&(t5->next), 1);
    //printList(t5);
    //printList(t4);

    //printList(sortedIntersect(t4, t5));
    recursiveReverse(&t5);
    printList(t5);
    return 0;
}
Пример #10
0
int main(){
	int data[10] = {9,6,3,2,5,8,4,7,1,0};
	int i;
	insertSort(data,10);
	for(i = 0; i < 10; i ++)
		printf("%d ",data[i]);
	printf("\n");
	return 0;
}
Пример #11
0
int main(int argc, char *argv[])
{
	// Make sure there is a filename argument to avoid error
	if (argc <= 1)
	{
		printf("No filename argument found, please ry again.\n");
		exit(0);
	}

	char *fileName = argv[1];
	int *list, count = 5, i = 0;
	list = (int*)malloc(count*sizeof(int));

	// Read numbers from file to list
	FILE* f = fopen (fileName , "r");
	while (!feof (f))
	{
		if (i >= count - 1)
		{
			count *= 2;
			list = realloc(list, count*sizeof(int));
		}
		fscanf (f, "%d", &list[i]);
		i ++;
	}
	fclose (f);
	count = i;
	list = realloc(list, count*sizeof(int));
	
	// Take input and choose sorting algorithm
	int selection = -1;
	while (selection < 0 || selection > 3)
	{
		printf("Select sorting algorithm (1-3):\n");
		printf("[1] Bubble sort\n");
		printf("[2] Shell sort\n");
		printf("[3] Insertion sort\n");

		scanf("%d", &selection);
	}
	switch(selection)
	{
		case 1:
			bubbleSort(list, count);
			break;
		case 2: 
			shellSort(list, count);
			break;
		case 3: 
			insertSort(list, count);
			break;
	}

	searchList(list, count);

	free(list);
}
Пример #12
0
int main(){
	int a[] = {0,12,35,62,58,93,23,16,17,45,11,5,6};
	
	insertSort(a,12);

	for(int i=1; i<=12; i++)
		printf("%d ",a[i]);
	return 1;
}
Пример #13
0
int main()
{
    static TYPE A[] = {5,6,7,8,9,2,3,4,5,6,1,2,3,4,5,6};
    int length = 16;
    int start = 0,end = length-1;
    insertSort(A,length);
    print_array(A,length);
    return 0;
}
Пример #14
0
int main(){
  int A[7]={5,1,2,6,4,3,9};
  insertSort(A,7);
  for(int i=0;i<7;i++){
    printf("%d ",A[i]);
  }
  printf("\n");
  return 0;
}
Пример #15
0
int main() {

    int A[] = {3, 2, 4, 14, 7, 6, 9};
    int n = sizeof(A) / sizeof(int);
    printArr(A, n);
    insertSort(A, n);
    printArr(A, n);
    
    return 0;
}
Пример #16
0
int main()
{
	int v[16] = { 13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10};
	int N = 16;
	for (int i = 0; i < N; i++)
		printf("%d, ", v[i]);
	printf("\n");
	insertSort(v, N);
	return 1;
}
Пример #17
0
int main(int argc, char const *argv[])
{
	int a[] = {2, 4, 1, 5, 3, 9, 7};
	insertSort(a, sizeof(a) / sizeof(int));
	for (int i = 0; i < sizeof(a) / sizeof(int); ++i)
	{
		printf("%d\t", *(a + i));
	}
	printf("\n");
	return 0;
}
Пример #18
0
int main() {
    int array[5] = {5,4,3,2,1};
    insertSort(array,5);

    int i;

    for(i = 0; i<5; i++) {
        printf("%d\t",array[i]);
    }

}
Пример #19
0
int main()
{
	float a[] = { 1.2, 8.5, 0.3, 4.3, 0.1, 5.5, 0.75, 3.2,
				  8.1, 3.3, 4.5, 9.5, 2.7, 7.3, 3.3 };

    printArr(std::cout, a, 15);
    insertSort(a, 15);
    std::cout << "Resulte:\n";
    printArr(std::cout, a, 15);
    
    return 0;	
}
Пример #20
0
int main(int argc, char ** argv)
{
  int arr[] = {38,65,97,76,13,27,49};
  int len = sizeof(arr)/sizeof(int);
  insertSort(arr, len);
  for(int i = 0; i < len; ++i){
    std::cout<<arr[i]<<std::endl;
  }

  system("pause");
  return 0;
}
Пример #21
0
void testInsertSort() {
	int sortArray1[]={3,5,8,9};
	int sortArray2[]={-5,-2,5,10,20};
	int sortArray3[]={2,3,5,5};
	int disOArray[]={5,9,8,3};
	int revOArray[]={9,8,5,3};
	int negArray[]={-5,5,10,-2,20};
	int repArray[]={5,2,3,5};

	printf("Test insertSort\n");

	printf("Test Disordered Array.\n");
	assert(memcmp(insertSort(disOArray,4),sortArray1,sizeof(disOArray))==0);

	printf("Test Reverse Ordered Array.\n");
	assert(memcmp(insertSort(revOArray,4),sortArray1,sizeof(revOArray))==0);

	printf("Test Negatives in Array.\n");
	assert(memcmp(insertSort(negArray,5),sortArray2,sizeof(negArray))==0);

	printf("Test Empty Array.\n");
	assert(insertSort(NULL,1)==NULL);

	printf("Test Repeats in Array.\n");
	assert(memcmp(insertSort(repArray,4),sortArray3,sizeof(repArray))==0);

	printf("Test Ordered Array.\n");
	assert(memcmp(insertSort(sortArray1,4),sortArray1,sizeof(sortArray1))==0);

	printf("Tests Passed\n");
}
Пример #22
0
int hashInsert(size_t value, size_t position)
{
	int l=1,c;
	size_t *p=hTable[value],h=p[0];

	/*
	 * check the elements whether have been appeared
	 * in the slideWindows
	 * hTable[value][0] save the all Occurrences witch
	 * value is value.
	 *
	 */

	if(h==0){
		p[0]++;
		p[1]=position;
	}else if(h>0){

		/*
		 * find the first place whose value just
		 * less than position a little.
		 *
		 */

		while(l<h){
			c=(l+h)>>1;
			if(p[c]<position)
				l=c+1;
			else if(p[c]>position)
				h=c-1;
			else
				h=c;
		}
		
		/*
		 * found! OK.
		 * the elements occurrences add 1 and take the
		 * first location's value to the last one.
		 * also,save the position in the plase witch just
		 * have found and insertion_sort the right of the
		 * array.
		 *
		 */

		p[0]++;
		p[p[0]]=p[l];
		p[l]=position;
		insertSort(p+l,p[0]-l+1);
	}

	return(0);
}
Пример #23
0
int main()
{
    int n;
    scanf("%d",&n);
    int *a = new int[n];
    for (int i = 0; i < n; i++) {
        scanf("%d",&a[i]);
    }
    insertSort(a,n);
    getMode(a,n);
    printf("%d %d",NUM,MAXCOUNT);
    return 0;
}
Пример #24
0
void chooseSortTest(int len)
{
	cout << " chooseSort" << endl;
	int* arr= createData(len);

	int start = clock();
	insertSort(arr,len);
	int end = clock();

	if (check(arr,len))
	{
		cout << "sort successful! time:" <<  (end - start) << endl << endl;
	}
}
Пример #25
0
void
test_insertSort01(void)
{
    long ary[3];

    // pre
    memset( ary, 0x00, sizeof(ary) );
    ary[0] = 0;
    ary[1] = 1;
    ary[2] = 2;

    insertSort( ary, sizeof(ary)/sizeof(ary[0]) );
    CU_ASSERT_EQUAL( 1, ary[1] );
    CU_ASSERT_EQUAL( 2, ary[2] );
}
Пример #26
0
int main()
{
	int a[MAX], n;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
		scanf("%d",&a[i]);
	
	insertSort(a, n);

	printf("Sorted Array : \n");
	for(int i = 0; i < n; i++)
		printf("%d ",a[i]);
	printf("\n");	
	return 0;
}
Пример #27
0
int main()
{
	int int_list[MAX_ELEMENTS];
	int size = 5000;

	initList(int_list, size);
	insertSort(int_list, size);

	initList(int_list,  size);
	bubbleSort(int_list, size);

	//printf("%d %d\n", int_list[0], int_list[size-1]);

	return 0;
}
Пример #28
0
//╡Бйт
int main()
{
	int i;
	int databuf[] = {10,  5, 6, 1, 0, 8, 11, 23, 14};

	insertSort(databuf, sizeof(databuf)/sizeof(int));

	for (i=0; i<sizeof(databuf)/sizeof(int); i++)
	{
		printf("%d  ", databuf[i]);
	}

	getchar();
	return 0;
}
Пример #29
0
//综合排序
//当数组长度>limit时  使用归并排序分治数组
//当数组长度<=limit时  使用插入排序排序数组
void multipleSort(int *data,int start,int end,int limit)
{
	int len = end - start + 1;
	//printf("len = %d  start = %d  end = %d 排序前\n",len,start,end);
	if(len <= limit )
	{
		insertSort(data, start, end);
	}else{//归并排序
		int mid = (start + end)/2;
		multipleSort(data, start, mid, limit);
		multipleSort(data, mid+1, end, limit);
		merge(data, start, mid, end);
	}//end if
	//printf("len = %d  start = %d  end = %d 排序end---\n",len,start,end);
}
Пример #30
0
int main()
{
    int sort[] = {8,2,10,11,32,19,100,21,83,76,92};
    int i,size = SIZE(sort);

    for(i = 0 ; i < size; ++i)
        printf("%d ",sort[i]);

    printf("\n\n");

    insertSort(sort,size);

    for(i = 0 ; i < size; ++i)
        printf("%d ",sort[i]);
}