int main(void) { int N; scanf("%d", &N); int arr[N], i; for(i = 0; i < N; i++) { scanf("%d", &arr[i]); } insertionSort(N, arr); return 0; }
int main(void) { double *pdNumbers; int iCount; int iArrayLength; double dNumber; int i; int iScanfRet; /* Read the integer that specifies how many numbers follow. */ iScanfRet = scanf("%d", &iArrayLength); if ((iScanfRet != 1) || (iArrayLength < 0)) { fprintf(stderr, "Improper number count\n"); exit(EXIT_FAILURE); } /* Create an array with the specified number of elements. */ pdNumbers = (double*)calloc((size_t)iArrayLength, sizeof(double)); if (pdNumbers == NULL) { fprintf(stderr, "Cannot allocate memory\n"); exit(EXIT_FAILURE); } /* Read the numbers into the array. */ for (iCount = 0; iCount < iArrayLength; iCount++) { iScanfRet = scanf("%lf", &dNumber); if (iScanfRet == 0) { fprintf(stderr, "Non-number in stdin\n"); exit(EXIT_FAILURE); } if (iScanfRet == EOF) break; pdNumbers[iCount] = dNumber; } /* Sort the array. */ insertionSort(pdNumbers, iCount); /* Write the numbers from the array. */ for (i = 0; i < iCount; i++) printf("%g\n", pdNumbers[i]); /* Free the array. */ free(pdNumbers); return 0; }
std::vector<UINT16> Kinectv2::getRawDepth() { rawDepth.resize(depthWidth * depthHeight); //std::vector<UINT16> rawDepth(depthWidth * depthHeight); for(int i = 0;i < depthWidth * depthHeight;i++) rawDepth[i] = 0; for(int k = 0;k < SAVE_SIZE;k++) for(int i = 0;i < depths[k].size();i++) { if(i < depthWidth || i % depthWidth == 0 || i % (depthWidth - 1) == 0 || i >= (depthHeight - 1) * depthWidth) depths[k][i] = depths[k][i]; else { UINT16 window[9]; window[0] = depths[k][i - depthWidth - 1]; window[1] = depths[k][i - depthWidth]; window[2] = depths[k][i - depthWidth + 1]; window[3] = depths[k][i - 1]; window[4] = depths[k][i]; window[5] = depths[k][i + 1]; window[6] = depths[k][i + depthWidth - 1]; window[7] = depths[k][i + depthWidth]; window[8] = depths[k][i + depthWidth + 1]; insertionSort(window); depths[k][i] = window[4]; } } //for(int i = 0;i < SAVE_SIZE;i++) //{ // for(int j = 0;j < depths[i].size();j++) // rawDepth[j] += depths[i][j]; //} // //for(int i = 0;i < rawDepth.size();i++) // rawDepth[i] /= SAVE_SIZE; //setCoordinate(rawDepth); // std::vector<UINT16> rawDepth(depthWidth * depthHeight); //for(int i = 0;i < depthWidth * depthHeight;i++) // rawDepth[i] = 0; // for(int j = 0;j < depths[9].size();j++) // rawDepth[j] = depths[9][j]; //setCoordinate(rawDepth); return rawDepth; }
int main(int argc, char* argv[]) { if (argc < 4) { printf("Numero de argumentos insuficiente\n"); printf("Uso: ./prog <tam_vetor> <chave> <tipo_busca>\n"); printf("\t<tipo> = 1 sequencial, 2 binaria\n\n"); return 0; } int n = atoi(argv[1]); // tamanho do vetor int chave = atoi(argv[2]); // numero a buscar nos dados int op = atoi(argv[3]); int * vet = geraVetorAleatorio(n); printf("exemplos: %d, %d\n", vet[n-2], vet[n-1]); long int pos; clock_t c1, c2; c1 = clock(); if (op == 1) { fprintf(stdout,"Realizando busca...\n"); fflush(stdout); pos = buscaSequencial(vet, n, chave); } else if (op == 2) { fprintf(stdout,"Ordenando...\n"); fflush(stdout); // bubbleSort(vet, n); // nao usar - dica do Obama insertionSort(vet, n); fprintf(stdout,"Realizando busca...\n"); fflush(stdout); pos = buscaBinaria(vet, 0, n-1, chave); } c2 = clock(); double time_diff = (c2-c1)/((double) CLOCKS_PER_SEC); printf("Tempo de execucao: %.4lf\n", time_diff); if (pos == -1) { printf("Valor nao encontrado\n\n"); } else { printf("Valor encontrado na posicao: %ld\n\n", pos); } free(vet); return 0; }
int* bucketSort(int arrA[], int arrSize) { int* arrB; // Two dimensional array for (int i = 1; i<arrSize; i++) { arrB[floor(n*arrA[i])] = arr[i]; } for (int i = 0; i<n-1;i++) { arrB[i][i] = insertionSort(arrB[i][i], arrSize); } return arrA; }
int main(void) { int _ar_size; scanf("%d", &_ar_size); int _ar[_ar_size], _ar_i; for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) { scanf("%d", &_ar[_ar_i]); } insertionSort(_ar_size, _ar); return 0; }
void testrandomlists(int n, int size) { for(int i=0; i<n; i++) { int list[size]; int check[size]; fill_random(list, size); list_copy(list, check, size); insertionSort(list, size); insertionSortSafe(check, size); if(!list_equal(list, check, size)) printf("FAIL!\n"); } }
int main() { // Usage and test int nums[] = {122, 111, 3, 125, 6, 21, 13, 13, 8}; int len = sizeof(nums) / sizeof(nums[0]); printf("unsorted: "); printArray(nums, len); insertionSort(nums, len); printf("sorted: "); printArray(nums, len); return 0; }
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int _ar_size;count=0; scanf("%d", &_ar_size); int _ar[_ar_size], _ar_i; for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) { scanf("%d", &_ar[_ar_i]); } insertionSort(_ar_size, _ar); printf ("%d\n",count); return 0; }
int main(){ int arr[size]; FILE * fp = fopen("number.txt", "r"); for(int i = 0; i < size; i++){ fscanf(fp, "%d", &arr[i]); } printf("\n"); insertionSort(size-1, arr); //merge(size-1, arr); for(int i = 0; i < size; i++){ printf("%d\n", arr[i]); } return 0; }
int main(int argc, char* argv[]) { #define SIZE 7 int arr[SIZE] = {2, 3, 1, 10, 8, 7, 9}; insertionSort(arr, SIZE); int i; for (i=0; i<SIZE; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
int main(void) { int _ar_size; scanf("%d", &_ar_size); int _ar[_ar_size]; for (int i = 0; i < _ar_size; i++) { scanf("%d", &_ar[i]); } insertionSort(_ar_size, _ar); return 0; }
int main(int argc, char *argv[]){ FILE *in; int i; int j; in = fopen(argv[1], "r"); if(in==NULL){ printf("unable to open file %s for reading\n", argv[1]); exit(1); } int numOfKeys; fscanf(in, "%d", &numOfKeys); int keys[numOfKeys]; int keys2[numOfKeys]; /*This will read in the numbers and set them inside both arrays*/ /*so they can later be sorted*/ for(i = 0; i < numOfKeys;i++){ fscanf (in, "%d", &j); keys[i] = j; keys2[i] = j; } heapSort(keys,numOfKeys); insertionSort(keys2,numOfKeys); /*This loop checks to see if both arrays are now sorted*/ for(i = 0; i < numOfKeys-1;i++){ if(keys[i] > keys[i+1]){ printf("NOT SORTED\n"); exit(1); } if(keys2[i] > keys2[i+1]){ printf("NOT SORTED\n"); exit(1); } } /*Print both now sorted arrays and end the program*/ printf("The heapSorted Array"); for(i = 0; i < numOfKeys;i++){ printf(" : %d",keys[i]); } printf("\nThe insertionSorted Array"); for(i = 0; i < numOfKeys;i++){ printf(" : %d",keys2[i]); } printf("\n"); fclose(in); return(0); }
int main(void) { // Read input int N; scanf("%d", & N); int arr[N], i; for (i = 0; i < N; i++) { scanf("%d", & arr[i]); } // Sorting insertionSort(N, arr); return 0; }
int main() { int* A = createArr(20, INT); print_arr(A, 20); int* B = createArr(10, INT); print_arr(B, 10); int i; int size = 10; for( i = 0 ; i < 10 ; i++) { insertionSort(A, B[i], i + 10); } print_arr(A, 20); return 0; }
int main(int argc, char *argv[]) { int values[] = {80, 50, 70, 10, 60, 20, 40, 30 }; int *pBuffer = NULL; printf("Before Sort\n"); printArray(values, 8); insertionSort(values, 8); printf("After Sort\n"); printArray(values, 8); return 0; }
int main(){ printf("Please input n:"); int n=0; scanf("%d",&n); int arr[INF]={0}; int i=0; for(i=0;i<n;i++){ scanf("%d",&arr[i]); } insertionSort(arr,n); for(i=0;i<n;i++){ printf("%d\n",arr[i]); } return 0; }
int main() { int tab[] = {6, 5, 2, 0, 1, 3, 4, 7, 7, 7, 6, 6, 5}; std::cout << "Before: "; for(int i : tab) std::cout << i << ' '; std::cout << std::endl; insertionSort(tab, 13); std::cout << "After: "; for(int i : tab) std::cout << i << ' '; std::cout << std::endl; return 0; }
int main() { int n,a[1000],i; printf("Enter the size of array\n"); scanf("%d",&n); printf("Enter the elements of array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); //call the insertionSort function insertionSort(a,n); printf("sorted elements by insertionsort\n"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; }
void menuOrdenacao() { int op = 0;; int vector[SIZE]; int vectorAux[SIZE]; readVector(SIZE, vector); printf("Vetor DESORNEDADO: "); printVector(SIZE, vector); do { op = menuDeOrdenacao(); switch(op) { case 1: copiaVetor(vector, vectorAux); bubbleSortRecursive(SIZE, vectorAux); printf("Vetor DESORNEDADO: "); printVector(SIZE, vector); printf("\nVetor ORDENADO."); printVector(SIZE, vectorAux); break; case 2: copiaVetor(vector, vectorAux); insertionSort(SIZE, vectorAux); printf("Vetor DESORNEDADO: "); printVector(SIZE, vector); printf("\nVetor ORDENADO."); printVector(SIZE, vectorAux); break; case 3: copiaVetor(vector, vectorAux); seletionSort(SIZE, vectorAux); printf("Vetor DESORNEDADO: "); printVector(SIZE, vector); printf("\nVetor ORDENADO."); printVector(SIZE, vectorAux); break; case 4: copiaVetor(vector, vectorAux); quickSort(SIZE, vectorAux); printf("Vetor DESORNEDADO: "); printVector(SIZE, vector); printf("\nVetor ORDENADO."); printVector(SIZE, vectorAux); break; case 5: menuPrincipal(); } } while(op!=5); }
void quickSort(ItemType theArray[], int first, int last) { if ((last − first + 1) < MIN_SIZE) { insertionSort(theArray, first, last); } else { // Create the partition: S1 | Pivot | S2 int pivotIndex = partition(theArray, first, last); // Sort subarrays S1 and S2 quickSort(theArray, first, pivotIndex − 1); quickSort(theArray, pivotIndex + 1, last); } // end if } // end quickSort
TEST(SORT, insertion) { const int COUNT = 10; int i, *after, *before; before = createValuesRandomly(COUNT); after = copyArrayValues(before, COUNT); insertionSort(after, COUNT); printf("before|after\n"); for (i = 0; i < COUNT; ++i) printf("%d%7d\n", before[i], after[i]); free(after); free(before); }
redirecionarFluxo(int valor){ switch(valor){ case 1: popularVetor(); break; case 2: imprimirVetor(); break; case 3: insertionSort(); break; case 4: break; } }
void topDownMergeSort(int A[], int *B, int begin, int end){ if (end - begin >= 2){ if (end - begin <= THRESHOLD){ printf("Se executa insertion sort \n"); insertionSort(A, begin, end); } else { int middle = (begin + end) / 2; topDownMergeSort(A, B, begin, middle); topDownMergeSort(A, B, middle, end); topDownMerge(A, begin, middle, end, B); copyArray(A, begin, end, B); } } }
int main() { int i; float a[] = {1,5,4,7,6,2,3,8,9,10}; insertionSort(a,10); for(i = 0; i<10; i++) { printf("%.0f ", a[i]); } printf("\n"); return 0; }
int main(void) { int *a, *b, tam, i, num; time_t seconds, start, end; double duration; unsigned long trocas = 0; tam = 100000; time(&seconds); srand((unsigned int) seconds); //printf("Digite o tamanho do vetor:"); //scanf("\n%d", &tam); a = (int*) malloc(tam*sizeof(int)); b = (int*) malloc(tam*sizeof(int)); for (i = 0; i < tam; i++) { num = gerarNumeroAleatorio(1000); a[i] = num; b[i] = num; } time(&start); trocas = selectionSort(a, tam); time(&end); duration = difftime(end, start); printf("\nDuration: %f seconds \tTrocas: %u\n", duration, trocas); system("PAUSE"); copy(b, a, tam); time(&start); trocas = insertionSort(a, tam); time(&end); duration = difftime(end, start); printf("\nDuration: %f seconds \tTrocas: %u\n", duration, trocas); system("PAUSE"); copy(b, a, tam); time(&start); trocas = bubbleSort(a, tam); time(&end); duration = difftime(end, start); printf("\nDuration: %f seconds \tTrocas: %u\n", duration, trocas); system("PAUSE"); return EXIT_SUCCESS; }
void testSortPerformance(int *origin, int size, int idx, int inteval) { const int START = idx; const int LIMIT = size; const int INTEVAL = inteval; int *clone, *extra, i; time_t start; double result[5]; clone = (int *) malloc(sizeof(int) * LIMIT); extra = (int *) malloc(sizeof(int) * LIMIT); printf("%s\t%s\t%s\t%s\t%s\t%s\n", "개수", "선택정렬", "삽입정렬", "퀵정렬", "합병정렬", "히프정렬"); for (i = START; i <= LIMIT; i += INTEVAL) { memcpy(clone, origin, sizeof(int) * LIMIT); start = clock(); selectionSort(clone, i); result[0] = (double) (clock() - start) / (CLOCKS_PER_SEC / 1000); memcpy(clone, origin, sizeof(int) * LIMIT); start = clock(); insertionSort(clone, i); result[1] = (double) (clock() - start) / (CLOCKS_PER_SEC / 1000); memcpy(clone, origin, sizeof(int) * LIMIT); start = clock(); quickSort(clone, 0, i - 1); result[2] = (double) (clock() - start) / (CLOCKS_PER_SEC / 1000); memcpy(clone, origin, sizeof(int) * LIMIT); start = clock(); mergeSort(clone, extra, i - 1); result[3] = (double) (clock() - start) / (CLOCKS_PER_SEC / 1000); memcpy(clone, origin, sizeof(int) * LIMIT); start = clock(); heapSort(clone, i - 1); result[4] = (double) (clock() - start) / (CLOCKS_PER_SEC / 1000); printf("%d\t%lf\t%lf\t%lf\t%lf\t%lf\n", i, result[0], result[1], result[2], result[3], result[4]); } free(clone); free(extra); }
int main (int argc, char **argv) { int array[10]; for(int i=0;i<10;i++) { array[i] = array[i]%10; } insertionSort(array); for(int i=0;i<10;i++) { printf("%i\n", array[i]); } }
int main (void) { int *v, n, i; printf("Inserire n: "); scanf("%d", &n); v = (int *)malloc(n * sizeof(int)); srand(time(NULL)); for(i=0; i<n; i++) { v[i] = rand()%10; printf("%d\t", v[i]); } printf("\n\n"); insertionSort(v, n); for(i=0; i<n; i++) printf("%d\t", v[i]); printf("\n"); return 0; }
int main(){ //variable declaration int arr[5], i; //input for(i = 0; i < 5; i++) scanf("%d", &arr[i]); //sort insertionSort(arr, 5); //passing arr address and no. of elements //output for(i = 0; i < 5; i++) printf("%d\n", arr[i]); return 0; }