Example #1
0
int main(void){
    clock_t begin, end;
    double timeElapsed;
    srand(time(NULL));
    int numNums;
    int i;
    scanf("%d",&numNums);
    int * A = malloc(sizeof(int)*numNums);
    for (i = 0; i < numNums; i++){
        A[i] = rand() % 10000000;
    }
    begin = clock();
    quickSort(A,0,numNums+1);
    end = clock();
    timeElapsed = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("\n time: %lf\n",timeElapsed);
    return 0;
}
Example #2
0
int main(){
	int waitSort[11];
	int i, input;
	printf("please input numbers(end by -1):\n");
	for(i = 0; i < 10; i++){
		scanf("%d", &input);
		if(input == -1)
		  break;
		else
		  waitSort[i] = input;
	}
	waitSort[i] = -1;
	quickSort(waitSort, 0, i-1);
	for(i = 0; waitSort[i] != -1; i++){
		printf("%d,", waitSort[i]);
	}
	printf("\n");
	return 0;
}
Example #3
0
int main(int argc, char **argv)
{
	printf("quickSort\n");
	int len,i;
	int *array;
	printf("input count of number:");
	scanf("%d",&len);
	array=(int *)malloc(sizeof(int)*len);
	memset((void *)array,0,4*len);//初始化为0
	printf("input numbers:");
	for( i=0; i<len; i++)
	{
		scanf("%d",&array[i]);
	} 
	display_array(0,len,array);
	quickSort(array,0,len-1);
	free(array);
	return 0;
}
Example #4
0
int main() {
    FILE *in = fopen("task.in", "r");
    FILE *out = fopen("task.out", "w");
    int len = scanLen(in);
    int array[len];
    
    for ( int i = 0; i < len; i++ ) {
        fscanf(in, "%d", &array[i]);
    }
    fclose(in);
    quickSort(array, 0, len-1);
    for ( int i = 0; i < len - 1; i++ ) {
        fprintf(out, "%d ", array[i]);
    }
    fprintf(out, "%d\n", array[len-1]);
    fclose(out);
    
    return 0;
}
Example #5
0
void ordenaVetor (int* vetor, int size, int algoritmo) {
  switch (algoritmo) {
    case 1:
      fakeSort (vetor, size);
      break;
    
    case 2:
      bubbleSort (vetor, size);
      break;

    case 3:
      quickSort (vetor, 0, size-1);
      break;

    default:
      copiaValores (mergeSort (vetor, 0, size-1), vetor, size);
      break;
  }
}
void main(int argc, char* argv[])
{
	//input
	init(S,SIZE);
	//before sorted
	print(S,SIZE);

	//calling sort interface
	quickSort(S,0,SIZE-1);
	//insertSort(S,0,SIZE-1);

	//after sorted
	print(S,SIZE);

	//pause on console
	getch();
	return;

}
Example #7
0
int main()
{
	int a[ELEM_N];
	randGenerator(a,ELEM_N,1000);

	printIntarray(a,ELEM_N);
//	insertionSort(a,ELEM_N);
//	selectSort(a,ELEM_N);
//	shellSort(a,ELEM_N);
//	bubbleSort(a,ELEM_N);
	quickSort(a,ELEM_N);
//	heapSort(a,ELEM_N);
//	radixSort(a,ELEM_N);
//	countingSort(a,ELEM_N);
//	mergeSort(a,ELEM_N);
	printIntarray(a,ELEM_N);

	return 0;
}
int main(){
	
	long long int n,c;	
	struct cont * arr;
	scanf("%lld",&n);
	arr=(struct cont *)calloc(n,sizeof(struct cont));
	c=0;
	while(c<n){
		scanf("%s",arr[c].name);	
		scanf("%lld",&arr[c].val);
		c++;
	}
	quickSort(arr,n);
	for(c=0;c<n;c++){
		printf("%s\n",arr[c].name);		
		printf("%lld\n",arr[c].val);
	}
	return 0;
}
Example #9
0
/***********************主函数************************/
void main()
{
    cout<<"请输入数组元素的个数(500~5000):";
    cin>>numArr;
    if(num==0)	exit(0);	//可以退出
    int compCount;
    for(compCount=0; compCount<numArr; compCount++)			//执行比较
    {
        getArr();
        selectionSort(compCount);
        insertionSort(compCount);
        bottomupSort(compCount);
        mergeSort(compCount);
        quickSort(compCount);
    }
    for(int sortMode=0; sortMode<5; sortMode++)
        statistics(sortMode);
    output();
}
Example #10
0
static void buildPrep(const QString &destinationFile)
{
  // Sort the links in the memory.
  // Sort comparison operator must not depend on locale, because the dictionary file
  // is the same in all systems.
  QList<Link> sortedLinks(links);
  quickSort(sortedLinks.begin(), sortedLinks.end(), Comparsion());

  // Final file.
  QFile file(destinationFile);
  file.open(QIODevice::WriteOnly);
  quint32 entryCount = links.size();
  file.write((const char*)&entryCount, sizeof(quint32));

  foreach(const Link &it, sortedLinks)
  {
    qint64 realOffset = it.second + sizeof(quint32) + entryCount * sizeof(qint64);
    file.write((const char*)&realOffset, sizeof(qint64));
  }
int minAbsSumPair2(int arr[],int res[][2],int size)
{
	int i=0,j=size-1,index=0,sum,min_sum=abs(arr[0]+arr[size-1]);
	res[index][0]=i;
	res[index][1]=j;
	quickSort(arr,0,size-1);
	while(i!=j)
	{
        sum=abs(arr[i]+arr[j]);
		if(min_sum>sum)//condition for finding min_sum pair
	    {
			res[index][0]=i;
			res[index][1]=j;
			min_sum=sum;
	    }
	    if(sum<0)
	    	i++;
		else
		    j--;
	}
	i=0;
	j=size-1;
	index=1;
    while(i!=j)
	{
        sum=arr[i]+arr[j];
		if(min_sum==abs(sum))//condition for finding min_sum pair
	    {
			if(res[0][0]!=i||res[0][1]!=j)
			{
				res[index][0]=i;
				res[index++][1]=j;
			}
	    }
	    if(sum>0)
	    	j--;
		else
		    i++;
	}
	res[index][0]=min_sum;
	return index;
}
Example #12
0
int main(int argc, const char *argv[])
{
    int array[N] = {2, 3, 8, 6, 5};

    //initArray(array, N);
    printArray(array, N);
    printf("\nPress Enter to start sort:\n");
    getchar();

    //bubbleArray(array, N);
    //bubbleArray2(array, N); //Wrong!!
    quickSort(array, 0, N-1);
    printf("\nSorted! Press Enter to print\n");
    getchar();

    printArray(array, N);

    printf("\n");
    return 0;
}
Example #13
0
/*
 * Check arguments, select an appropriate implementation,
 * cast the array to char * so that array+i*itemSize works.
 */
U_CAPI void U_EXPORT2
uprv_sortArray(void *array, int32_t length, int32_t itemSize,
               UComparator *cmp, const void *context,
               UBool sortStable, UErrorCode *pErrorCode) {
    if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
        return;
    }
    if((length>0 && array==NULL) || length<0 || itemSize<=0 || cmp==NULL) {
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
        return;
    }

    if(length<=1) {
        return;
    } else if(length<MIN_QSORT || sortStable) {
        insertionSort((char *)array, length, itemSize, cmp, context, pErrorCode);
    } else {
        quickSort((char *)array, length, itemSize, cmp, context, pErrorCode);
    }
}
Example #14
0
int main(){
	
	int array[]={5,7,8,1,6,2};
	int size,i;
	size=MAX;
	for (i=0;i<MAX;i++){
		printf("%d \t",array[i]);
	}
	
	printf("\n ");
	
	quickSort(array,0,MAX-1);
	
	printf("\n HUGAGAG");
	for (i=0;i<MAX;i++){
		printf("%d \t",array[i]);
	}
	getch();
	return 1;
}
Example #15
0
/**
    Sort the array using the supplied compare function

    function sort(compare: Function = null, direction: Number = 1): Array

    Where compare is defined as:
        function compare(a,b): Number
 */
PUBLIC EjsArray *ejsSortArray(Ejs *ejs, EjsArray *ap, int argc, EjsObj **argv)
{
    EjsFunction     *compare;
    int             direction;

    if (ap->length <= 1) {
        return ap;
    }
    compare = (EjsFunction*) ((argc >= 1) ? argv[0]: NULL);
    if (compare == ESV(null)) {
        compare = 0;
    }
    if (compare && !ejsIsFunction(ejs, compare)) {
        ejsThrowArgError(ejs, "Compare argument is not a function");
        return 0;
    }
    direction = (argc >= 2) ? ejsGetInt(ejs, argv[1]) : 1;
    quickSort(ejs, ap, compare, direction, 0, ap->length - 1);
    return ap;
}
Example #16
0
struct Interval* merge(struct Interval* intervals, int intervalsSize, int* returnSize) {
    if (intervalsSize <= 1) {
        (* returnSize) = intervalsSize;
        return intervals;
    }
    quickSort(intervals, 0, intervalsSize-1);
    (* returnSize) = 0;
    int i;
    for (i = 1 ; i < intervalsSize ; i++) {
        if (intervals[i].start > intervals[(* returnSize)].end) {
            (* returnSize)++;
            intervals[(* returnSize)].start = intervals[i].start;
            intervals[(* returnSize)].end = intervals[i].end;
        } else {
            intervals[(* returnSize)].end = max(intervals[(* returnSize)].end, intervals[i].end);
        }
    }
    (* returnSize)++;
    return intervals;
}
void merge2into1()
{
	FILE *fp[2], *fp1;
	char filename[5];
	int l,x,j,n;
	for(j=0;j<2;j++)
	{
		sprintf(filename,"%d",j+201);
		fp[j] = fopen(filename,"r");
		if(fp[j] == NULL)
		{
			printf("Error in opening file %d\n",j+201);
			return;
		}
	}
			
	fp1 = fopen("BigfileOutput","w");
	for(x=0;x<32;x++)
	{
		int m=0,a1[2*INTEGERS]={0};
		for(j=0;j<2;j++)
		{
			for(l=0;l<INTEGERS;l++)
			{
				fscanf(fp[j],"%d",&a1[m++]);
			}
		}
		quickSort(a1,0,2*INTEGERS-1);
		for(n=0;n<2*INTEGERS-1;n++)
		{
			fprintf(fp1,"%d\n",a1[n]);
		}
	}
	
	for(j=200+1;j<=202;j++)
	{
		sprintf(filename,"%d",j);
		remove(filename);
	}
	fcloseall();
}
int main()
{
	int n,i;
	int m;
	scanf("%d",&n);
	for(i=1;i<=n;++i)//a[0]不用
	{
		scanf("%d",score+i);
	}
	scanf("%d",&m);
	quickSort(1,n);
	for(i=1;i<=n;++i)
	{
		if(m==score[i])
		{
			printf("%d\n",i);
			break;
		}
	}
	return 0;
}
Example #19
0
File: 2.c Project: liuyanfight/CS50
int main(void)
{
    // input numbers
    int n;
    while (scanf("%d",&n) != EOF)
    {
    struct student s[n];
    for (int i = 0; i < n; i++)
    {
        scanf("%s %d %d", s[i].name, &s[i].age, &s[i].grade);
    }
    //quick sort
    quickSort(s, 0, n - 1);
    // print the result
    for (int i = 0; i < n; i++)
    {
        printf("%s %d %d\n", s[i].name, s[i].age, s[i].grade);
    }
    }
    return 0;
}
Example #20
0
int main(int argc, char **argv)
{
	if (argc != 2) {
		fprintf(stderr, "Enter length of array.\n");
		return EXIT_FAILURE;
	}
	int length = atoi(argv[1]);

	int *A = (int*)malloc(length * sizeof(int));
	
	if (A == NULL) {
		return EXIT_FAILURE;
	}

	randomInitialization(A, length);
	printArray(A, length);
	quickSort(A, 0, length - 1, length);
	printArray(A, length);
	free(A);
	return EXIT_SUCCESS;
}
int findSingleOccurenceNumber(int *A, int len)
{
	if (A == NULL || len < 0)
		return -1;
	quickSort(A, 0, len - 1);/*sorting the array using quick sort algorithm*/
	int x=A[0];                    
	for (int i = 1; i < len; i++)
	{
		if (i ==(len - 1))
		{
			if (A[i - 1] != A[i])
				x = A[i];
			continue;
		}
		if (A[i + 1] != A[i] && A[i - 1] != A[i])/*checking the predessor and successor 
												   the current element*/
			x = A[i];

	}
	return x;
}
int main()
{
	int stack[10], range;
	stack[0] = 1;
	stack[1] = 4;
	stack[2] = 3;
	stack[3] = 2;
	stack[4] = 5;
	stack[5] = 9;
	stack[6] = 10;
	stack[7] = 8;
	stack[8] = 6;
	stack[9] = 7;
    int size = sizeof(stack) / sizeof(stack[0]);

	quickSort(stack, 0, 9);
	pr(stack, 10);
	printf("Hello, World!\n");	
    printf("Hello, World!\n");
    return 0;
}
Example #23
0
File: qsort.c Project: borcun/free
int main() 
{
  const int size = 20;
  int arr[size] = {18, 10, 19, 6, -1, 4, 4, 7, -9, 0, 11, 12, -6, 8, -8, 1, 5, 22, 3, 20};
  int i;

  printf("Unsorted array : ");
  for(i = 0; i < size ; ++i)
    printf("%d ", arr[i]);

  // call quick sort function
  quickSort(arr, 0, size - 1);

  printf("\nSorted array   : ");
  for(i = 0 ; i < size ; ++i)
    printf("%d ", arr[i]);

  printf("\n");

  return 0;
}
Example #24
0
int main(int argc, char **argv){

	if (argc != 4){
		printf("Uso: ./quickSort archivoEntrada archivoSalida numeroDatos\n");
		return EXIT_FAILURE;
	}
	clock_t start, end;
	double cpu_time_used;
	int *A, cantidadDatos;
	cantidadDatos = atoi(argv[3]);
	A = (int*)malloc(sizeof(int)*cantidadDatos);
	readData(A,cantidadDatos,argv[1]);
	start = clock();
	quickSort(A,0,cantidadDatos-1);
	end = clock();
	cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
	printf("Time elapsed in seconds: %.5f\n", cpu_time_used);
	writeData(A,cantidadDatos,argv[2]);
	free(A);
	return 0;
}
Example #25
0
int main(int argc, char* argv[])
{
    int len =   atoi(argv[1]);
    int array[len],array2[len];
    srand(time(NULL));
    int i;
    for(i=0;i<len;i++)  {array[i]=rand()%10000; array2[i]=array[i];}

    printf("排序前的数组---->\n");
    for(i=0;i<len;i++) printf("%d  ",array[i]); printf("\n");

    quickSort(array,0,len-1);
    printf("递归方法排序后的数组---->\n");
    for(i=0;i<len;i++) printf("%d  ",array[i]); printf("\n");

    quickSort2(array2,len);
    printf("循环方法排序后的数组---->\n");
    for(i=0;i<len;i++) printf("%d  ",array2[i]); printf("\n");

    return 0;
}
Example #26
0
int main(){

int l,r, n, a, count = 0;
int arr[2000] = {0}; // initialize all values of arr to 0

while(1){

scanf("%d", &n);

if(n == 0)
break;

for(a = 0; a < n; a++){

scanf("%d", &arr[a]);

}

quickSort(arr, 0, n-1);

for(a = n-1; a >= 2; a--){
l = 0;
r = a - 1;

while(l < r){
if(arr[l] + arr[r] < arr[a]){
count += r-l;
l++;
}
else{
r--;
}
}

}	
printf("%d\n", count);
count = 0;
}
return 0;
}
Example #27
0
void enclave_main()
{
    int i;

    srand(time(NULL));
    for (i = 0; i < N_OBJ; i++) {
        int type = rand() % N_TYPE;
        int size = 0;
        switch (type) {
            case MEM_TYPE_SMALL:
                size = rand() % (3*SIZE_SMALL) + SIZE_SMALL;
                break;
            case MEM_TYPE_LARGE:
            case MEM_TYPE_MID:
                size = rand() % (3*SIZE_MID) + SIZE_MID;
                break;
                /*
            case MEM_TYPE_LARGE:
                size = rand() % (3*SIZE_LARGE) + SIZE_LARGE;
                break;
                */
            default:
                exit(1);
        }
        obj[i].buf = (unsigned long)malloc(size);
        obj[i].size = size;
    }

    quickSort(obj, 0, N_OBJ-1);

    for (i = 1; i < N_OBJ; i++) {
        unsigned long boundary = obj[i-1].buf + (unsigned long)obj[i-1].size;
        if (boundary > obj[i].buf)
            printf("Err: %lx > %lx\n", boundary, obj[i].buf);
    }

    printf("Malloc test pass\n");
    sgx_exit(NULL);
}
Example #28
0
void testAlgorithms() {
    int i;
    // Initialize + Display random array
    int array[ARRAY_LEN];
    randomize(array, ARRAY_LEN, ARRAY_MAX);

    // Sort array + Test it
    quickSort(array, 0, ARRAY_LEN-1);
    for (i=1; i < ARRAY_LEN; i++) {
        assert(array[i-1] <= array[i]);
    }
    printf("Sorting: Success\n");

    // Test binary search
    for (i=0; i < ARRAY_LEN; i++) {
        int index = binarySearch(array, 0, ARRAY_LEN-1, array[i]);
        assert(array[index] == array[i]);
    }
    assert(binarySearch(array, 0, ARRAY_LEN-1, ARRAY_MAX+1) == -1);
    assert(binarySearch(array, 0, ARRAY_LEN-1, -10) == -1);
    printf("Searching: Success\n");
}
Example #29
0
void Sensor_Task(void)
{
	double r, InRs, tmp, adc;
	WORD chamber = ReadTemperature(ADC_CHAMBER);
	WORD photodiode = ReadPhotodiode();
	WORD index = 0;

	// save the adc value by high low type
	chamber_h = (BYTE)(chamber>>8);
	chamber_l = (BYTE)(chamber);
	photodiode_h = (BYTE)(photodiode>>8);
	photodiode_l = (BYTE)(photodiode);

	// temperature calculation
	index = (WORD)((chamber/4) * 2.);
	currentTemp = (float)(pTemp_Chamber[index]) + (float)(pTemp_Chamber[index+1] * 0.1);

	if( compensation > 0 ){
		currentTemp = currentTemp * compensation;
	}

	// Checking overheating
	if( currentTemp >= OVERHEATING_TEMP ){
		currentState = STATE_READY;
		Stop_Task();
		currentError = ERROR_OVERHEAT;
	}
	
	// for median filtering
	temp_buffer[0] = temp_buffer[1];
	temp_buffer[1] = temp_buffer[2];
	temp_buffer[2] = temp_buffer[3];
	temp_buffer[3] = temp_buffer[4];
	temp_buffer[4] = currentTemp;

	memcpy(temp_buffer2, temp_buffer, 5*sizeof(float));

	currentTemp = (float)quickSort(temp_buffer2, 5);
}
Example #30
0
int main() 
{ 
    int arr1[] = {10, 7, 8, 9, 1, 5}; 
    int n1 = sizeof(arr1)/sizeof(arr1[0]); 
    quickSort(arr1, 0, n1-1); 
    printf("Quick sort: "); 
    printArray(arr1, n1);

    int arr2[] = {10, 7, 8, 9, 1, 5};
    int n2 = sizeof(arr2)/sizeof(arr2[0]); 
    selectionSort(arr2, n2);
    printf("Selection sort: "); 
    printArray(arr2, n2);

    int arr3[] = {10, 7, 8, 9, 1, 5};
    int n3 = sizeof(arr3)/sizeof(arr3[0]); 
    insertionSort(arr3, n3);
    printf("Insertion sort: "); 
    printArray(arr3, n3);

    return 0; 
}