// a.out size range int main (int argc, char *argv[]) { const int size = atoi(argv[1]); const int range = atoi(argv[2]); srand(time(NULL)); unsigned int *array = new unsigned int[size]; for (int i=0;i<size;i++) array[i] = rand() % range; struct timeval tp; int rtn = gettimeofday(&tp, NULL); double start = (double) tp.tv_sec+(1.e-6)*tp.tv_usec; countingSort(array, size, range); rtn = gettimeofday(&tp, NULL); double stop = (double) tp.tv_sec+(1.e-6)*tp.tv_usec; for (int i=1;i<size;i++) if (array[i] < array[i-1]) { std::cout << "Array was not successfully sorted!\n"; break; } std::cout << "Sorted " << size << " elements in " << stop - start << " seconds.\n"; delete[] array; return 0; }
int main() { int n, j, testcases, range; scanf("%d",&testcases); while(testcases--) { scanf("%d",&range); scanf("%d",&n); int *arr=(int*)malloc(n*sizeof(int)); for(j=0; j<n; j++) scanf("%d",arr+j); printf("\nInput"); printArray(arr,n); int *sorted = countingSort(arr,n,range); printf("\nCounting Sort"); printArray(sorted,n); free(arr); free(sorted); printf("\n"); } printf("\n"); return 0; }
void radixSort(int array[], int length, int digit, int k) { int num = 1; int* radix = (int*)calloc(length, sizeof(int)); if (radix == NULL) { printf("allocate memery failed\n"); exit(-1); } //counting sort not sort in place, we need another array to save the source array int* array_temp = (int*)calloc(length, sizeof(int)); if (array_temp == NULL) { printf("allocate memery failed\n"); exit(-1); } for (int i = 0; i < digit; i++) { for (int j = 0; j < length; j++) { radix[j] = (array[j] / num) % 10; array_temp[j] = array[j]; } countingSort(length, k, radix, array_temp, array); printf("sort by digit %d:\n", i + 1); printArray(array, length); num *= 10; } free(radix); }
int main() { char str[] = "ababababab"; countingSort(str); printf("Sorted string is : \n%s\n",str); return 0; }
void BuildSA() { for(int H = 1; H < A.size() ; H<<=1) { countingSort(H); countingSort(0); int nRA[N] , rank = nRA[ SA[0] ] = 0; for(int i = 1; i < N; ++i) { if(RA[ SA[i] ] != RA[ SA[i-1] ])rank++; else if(SA[i-1]+H >= N || SA[i]+H >= N)rank++; else if(RA[ SA[i]+H ] != RA[ SA[i-1]+H ])rank++; nRA[ SA[i] ] = rank; } memcpy(RA,nRA,sizeof(nRA)); } }
void buildSA() { VI nRA(N); for (int H = 1; H < N; H <<= 1) { countingSort(H); countingSort(0); int rank = nRA[SA[0]] = 0; for (int i = 1; i < N; ++i) { if (RA[SA[i]] != RA[SA[i - 1]]) rank++; else if (SA[i - 1] + H >= N || SA[i] + H >= N) rank++; else if (RA[SA[i] + H] != RA[SA[i - 1] + H]) rank++; nRA[SA[i]] = rank; } copy(nRA.begin(), nRA.end(), RA.begin()); } }
int main() { int mas[] = {4, 9, 8, 1, 4, 1, 4}; int n = 7; printMas(mas, n); countingSort(mas, n); printMas(mas, n); return 0; }
/** Sample code for Figure 4-17 Counting Sort fact sheet. */ int main (int argc, char **argv) { int i; int ar[NUM] = { 3, 0, 2, 0, 0, 2, 2}; countingSort(ar, NUM, 4); for (i = 0; i < NUM-1; i++) { printf ("%d ", ar[i]); } printf ("\n"); for (i = 0; i < NUM-1; i++) { assert (ar[i] <= ar[i+1]); } return 0; }
int main(int argc, const char * argv[]) { int * arr; int * res; int i; for(i = 0; i < 11; i++) { arr = createRandomArray(i); res = createRandomArray(i); printArray(arr, i); countingSort(arr, res, i, BOUND); printArray(res, i); printf("isSorted: %d\n", isSorted(res, i)); printf("---------------------------\n"); free(arr); free(res); } return 0; }
void main() { int A[]={0,1,5,2,2,4,4,5,7,8,9}; int i,n,k; n=sizeof(A)/sizeof(int); int B[n]; for(i=0;i<n;i++) { B[i]=0; } k=maximum(A,n); countingSort(A,B,k,n); for(i=0;i<n;i++) { printf("%d ",B[i]); } printf("\n"); }
//0.5 500 20 d:\Tamop2_4c\TechnicalReport\progik\map.ppm paralellm.ppm 0 1 csr 0 void EtGcSegment::segment(image<rgb>*dest, float c, int minSize, std::string sortingMethod){ EtTimer tmr; // sort edges by weight if( !sortingMethod.compare( "s" ) ){ tmr.start(); std::sort( edges, edges + this->numEdges); tmr.stop(); std::cout << imgW << " " << imgH << " std::sort " << tmr.getElapsedTime() << endl; } else if( !sortingMethod.compare( "csr" ) ){ tmr.start(); countingSort(); tmr.stop(); std::cout << imgW << " " << imgH << " CountingSort_round " << tmr.getElapsedTime() << endl; } else{ //if( !sortingMethod.compare( "csf" ) ) tmr.start(); countingSortFloor(); tmr.stop(); std::cout << imgW << " " << imgH << " CountingSort_floor " << tmr.getElapsedTime() << endl; } tmr.start(); segmentGraph( c ); tmr.stop(); std::cout << imgW << " " << imgH << " Disjoint " << tmr.getElapsedTime() << endl; if( doPostprocess ){ tmr.start(); postProcessOriginal( minSize ); tmr.stop(); std::cout << imgW << " " << imgH << " PostProcess " << tmr.getElapsedTime() << endl; } drawSegments( dest ); }
int main() { //Print initial array printf("\n//=====Shuffled array===================//"); setarray(); printarray("Initial Array"); //=====Simple sorts=====// printf("\n\n//=====Simple sorts====================//"); //Insertion sort setarray(); insertionSort(array,10); printarray("InsertionSort"); //Selection sort setarray(); selectionSort(array,10); printarray("SelectionSort"); //=====Eficient Sorts=====// printf("\n\n//=====Eficient sorts==================//"); //Mergesort setarray(); mergeSort(array,0,9,10); printarray("MergeSort"); //Heapsort setarray(); heapSort(array,10); printarray("HeapSort"); //Quicksort setarray(); quickSort(array,0,9); printarray("QuickSort"); //Bubble sort and variants printf("\n\n//=====Bubble sort and variants========//"); //Bubblesort setarray(); bubbleSort(array,10); printarray("BubbleSort"); //Shellsort setarray(); shellSort(array,10); printarray("ShellSort"); //Combsort setarray(); combSort(array,10); printarray("CombSort"); //Distribution sorts printf("\n\n//=====Distribution sorts==============//"); //Countingsort setarray(); countingSort(array, 10, 0, 99); printarray("CountingSort"); //Radixsort setarray(); radixSort(array, 10); printarray("RadixSort"); //Other sorts printf("\n\n//=====Other sorts=====================//"); //Gnomesort setarray(); gnomeSort(array,10); printarray("GnomeSort"); //Cyclesort setarray(); cycleSort(array,10); printarray("CycleSort"); printf("\n\n"); }
void radixSort(int A[], int n, int Max){ int e; for (e=1; Max/e>0; e*=10) countingSort(A, n, e); }
BYTE getMedianValue(BYTE* nums, int numsSize, int maxValue){ BYTE* sortedNums = countingSort(nums, numsSize, maxValue); return sortedNums[numsSize/2]; }
int main() { const unsigned long int N = 100; std::cout << N << std::endl; srand((int) time(NULL)); int * a = new int[N]; int * aux = new int[N]; copiaNumeros(a,N); for (int i = 0; i < N; ++i) { aux[i] = a[i]; } auto begin = std::chrono::high_resolution_clock::now(); bubbleSort(a, N); auto end = std::chrono::high_resolution_clock::now(); auto tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Bubble: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); cocktailSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Cocktail: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); insertionSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Insertion: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); bucketSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Bucket: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); countingSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Counting: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); mergeSort(a, 0, N-1, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Merge: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); radixSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Radix: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); shellSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Shell: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); selectionSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Selection: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); quickSort(a, 0, N-1); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Quick: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); heap_sort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Heap: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); binarySort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Binary: "<<tiempo.count()<<std::endl; delete a; delete aux; return 0; }