/* ** Divide the array into N/M blocks of size of M, sort every block ** using selection sort, and run the array merging the first block ** with the second, then the second block with the third, and so on. */ void merge_sort(item_t *array,int left,int right) { int n=right-left+1; int n_blocks=n/M; int *nodes; int i; if(right-left+1<M){ selection_sort(array,left,right); return; } nodes=malloc(n_blocks*sizeof(*nodes)); for(i=0;i<n_blocks;i++){ if(i==n_blocks-1){ nodes[i]=right; }else{ nodes[i]=left+(i+1)*M-1; } } for(i=0;i<n_blocks;i++){ selection_sort(array,left+i*M,nodes[i]); } for(i=0;i<n_blocks-1;i++){ merge(array,left,nodes[i],nodes[i+1]); } free(nodes); }
int main() { int a[] = {24,54,32,-56,12,-6,543,23,-85,32,6,65}; const int sa = sizeof(a) / sizeof(int); char b[] = { 'd', 'c', 'r', 's', 'j', 't' }; const int sb = sizeof(b) / sizeof(char); float c[] = { 4.32f, 5.4f, 0.01f, -56.9f, 54.34f, -34.54f }; const int sc = sizeof(c) / sizeof(float); long d[] = { 1 }; selection_sort(a, sa); selection_sort(b, sb); selection_sort(c, sc); selection_sort(d, 1); for (int i = 0; i < sa; i++) std::cout << a[i] << ' '; std::cout << std::endl; for (int i = 0; i < sb; i++) std::cout << b[i] << ' '; std::cout << std::endl; for (int i = 0; i < sc; i++) std::cout << c[i] << ' '; std::cout << std::endl; std::cout << d[0] << std::endl; return 0; }
int main(void) { int nums0[] = {31, 2, 5, 15, 21, 22, 11, 8, 1}; int sz0 = 9; print_array(nums0, sz0); selection_sort(nums0, sz0); print_array(nums0, sz0); int nums1[] = {1}; int sz1 = 1; print_array(nums1, sz1); selection_sort(nums1, sz1); print_array(nums1, sz1); int nums2[] = {}; int sz2 = 0; print_array(nums2, sz2); selection_sort(nums2, sz2); print_array(nums2, sz2); int nums3[] = {18, 23, 12, 13}; int sz3 = 4; print_array(nums3, sz3); selection_sort(nums3, sz3); print_array(nums3, sz3); }
void main() { clrscr(); int a[20],n,flag=0; cout<<"\n Enter the no of terms:"; cin>>n; cout<<"\n Enter elements:"; for(int i=0;i<n;i++) { cin>>a[i]; } cout<<"\nArray is:"<<endl; for(i=0;i<n;i++) { cout<<a[i]<<" "; } for(i=0;i<n-1;i++) { if(a[i]<a[i+1]) { flag=0; } else { flag=1; break; } } if(flag==0) cout<<"\nArray is already sorted"; else selection_sort(a,n); getch(); }
int main() { int i, array_size; printf("Enter number of elements: "); scanf("%d",&array_size); int a[array_size]; printf("Enter array: "); for(i=0; i<array_size; i++) scanf("%d",&a[i]); //reads a list of numbers from user and puts them in an array selection_sort(array_size, a); //calls selection sort () to sort array printf("Sorted array: "); for(i=0; i<array_size; i++) printf("%d ",a[i]); printf("\n"); //prints sorted array return 0; }
void selection_sort(int n, int array[]) { int big, i; if(n==0) return; //if the first parameter is 0, then the size of the array is 0 and the //function terminates big=array[0]; //initializes 'big' value to the first array entry for(i=1; i<n; i++) { if(array[i]>big) big=array[i]; } //finds largest number in array and makes big = largest value for(i=0; i<n; i++) { if(array[i]==big) { array[i]=array[n-1]; array[n-1]=big; break; } } //swaps position of largest value and last value in array selection_sort(n-1, array); //function calls self recursively to sort the remaining //numbers of the array. By decrementing n by 1, we ensure that the largest //value at the end will go untouched, while the remaining // values will be sorted. When n reaches 0, the function terminates. }
main() { clock_t tempo1; //Contagem de tempo do BubbleSort cria_vetor(); tempo1 = clock(); bubble_sort(); tempo1 = clock() - tempo1; printf("\nBubbleSorte: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC); printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario //-------------------------------------------------------------------------- //Contagem de tempo do InsertionSort cria_vetor(); tempo1 = clock(); insertion_sort(); tempo1 = clock() - tempo1; printf("InsertionSort: %.4f seg\n",(float)tempo1/CLOCKS_PER_SEC); printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario //-------------------------------------------------------------------------- //Contagem de tempo do SelectionSort cria_vetor(); tempo1 = clock(); selection_sort(); tempo1 = clock() - tempo1; printf("SelectionSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC); printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario //-------------------------------------------------------------------------- //Contagem de tempo do Heapsort cria_vetor(); tempo1 = clock(); heapsort(); tempo1 = clock() - tempo1; printf("HeapSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC); printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario //-------------------------------------------------------------------------- //Contagem de tempo do MergeSort cria_vetor(); tempo1 = clock(); mergesort(0, TAM - 1); tempo1 = clock() - tempo1; printf("mergeSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC); printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario //-------------------------------------------------------------------------- //Contagem de tempo do QuickSort cria_vetor(); tempo1 = clock(); quicksort(0, TAM - 1); tempo1 = clock() - tempo1; printf("QuickSort: %.4f seg \n",(float)tempo1/CLOCKS_PER_SEC); printf("Vetor ordenado? %s\n\n", verifica_ordem() ? "Sim" : "Nao"); //Operador ternario //-------------------------------------------------------------------------- getch(); }
int main(void) { int v[21]; int n=20; count c; setlocale (LC_ALL, "Portuguese"); resetVetor(v, n); printf("Vetor usado \n"); imprimeVetor(v, n); printf("selection Sort \n"); c = selection_sort(v, n); automato( c, v, n); printf("Selecion Sort v2 \n"); c = selection_sort2(v, n); automato( c, v, n); printf("Insertion Sort \n"); c = insertion_sort(v, n); automato( c, v, n); printf("Bubble Sort \n"); c = bubble_sort(v, n); automato( c, v, n); printf("Merge Sort \n"); c = merge_sort(v, 0, n); automato( c, v, n); return 0; }
void selection_sort(RandomAccessIterator begin, RandomAccessIterator end) { // typedef typedef std::iterator_traits<RandomAccessIterator> traits; typedef typename traits::value_type value_type; selection_sort(begin, end, std::less<value_type>()); }
int main(){ int A[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 }; int B[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; size_t len = sizeof(A) / sizeof(A[0]); selection_sort(A, len); selection_sort(B, len); printf("A: "); printl(A, len); printf("B: "); printl(B, len); return 0; }
long timeSelectionSort( std::size_t Size, std::uniform_int_distribution<IntType> &UniformIntDistribution_, std::default_random_engine DefaultRandomEngine_) { auto L = getRandomList(Size, UniformIntDistribution_, DefaultRandomEngine_); return timeAlgorithm([&]() -> void { L.selection_sort(); }); }
main() { // Function declaration void selection_sort(int *, int); int *array = NULL; // Pointer to int, initialize to nothing. int n, i; printf("Enter number of elements: "); scanf("%d", &n); // Dynamically allocate memory for n elements array = (int *)calloc(n, sizeof(int)); // Reading the elements printf("\nEnter the elements:"); for (i = 0;i < n;i++) scanf("%d", &array[i]); // Invoke selection_sort selection_sort(array, n); // Display sorted array printf("sorted array:"); for (i = 0;i < n;i++) printf("%d ", array[i]); printf("\n"); }
void sort_array(int *a, int n) { char c; int success; do { printf("\n\nSorting algorithms: "); printf("\n[1].-Bubble\n[2].-Selection\n[3].-Insertion\n[4].-No-Sorting"); printf("\nChoose:\t"); if(!(success = is_single_char(&c))) fflushstdin(); if(c < 49 || c > 52) printf("Please give a valid option\n"); } while(!success || c < 49 || c > 52); switch (c) { case '1': bubble_sort (a, n); break; case '2': selection_sort(a, n); break; case '3': insertion_sort (a, 0, n); break; case '4': printf("\nDATA STAYS THE SAME\n"); break; default: break; } }
END_TEST START_TEST (test_selection_rand) { selection_sort(randarray, N, compare_ints, exch_ints); ck_assert(is_sorted(randarray, N)); }
void par_mergesort(void * arg) { struct array * A = (struct array*)arg; if(A->len <= seq_threshold) { selection_sort(A); } else { struct array left_half, right_half; if (A->len % 2 == 0) { left_half.len = right_half.len = A->len/2; } else { left_half.len = A->len/2; right_half.len = A->len/2 + 1; } left_half.arr = A->arr; right_half.arr = A->arr + left_half.len; struct thread * left_t = thread_fork(par_mergesort, &left_half); struct thread * right_t = thread_fork(par_mergesort, &right_half); thread_join(left_t); thread_join(right_t); merge(&left_half, &right_half); } }
int main(void) { int *i; int a[N]; printf("Enter %d numbers to be sorted: \n", N); for (i = a; i < a + N; i++) /* fill sorted array with pointer arithmetic */ { scanf("%d\n", &*i); } selection_sort(a, N); /* perform selection sort recursivly with pointer arithmetic */ printf("In sorted order:"); for (i = a; i < a + N; i++) /* print sorted array */ { printf(" %d", *i); } printf("\n"); return 0; }
END_TEST START_TEST (test_selection_sort) { selection_sort(sorted, N, compare_ints, exch_ints); ck_assert(is_sorted(sorted, N)); }
int main(void){ Item a[10]; rand_array(a); printf("before: "); print_array(a); selection_sort(a,0,9); printf("after: "); print_array(a); rand_array(a); printf("before: "); print_array(a); insertion_sort(a,0,9); printf("after: "); print_array(a); rand_array(a); printf("before: "); print_array(a); quicksort(a,0,9); printf("after: "); print_array(a); }
int main() { int x[10] = {10,8,6,4,2,1,3,5,7,9}; //printArray(x,10); selection_sort(x, 10); }
int main(void) { /* int my_array[ARRAY_MAX]; int i, count = 0; while (count < ARRAY_MAX && 1 == scanf("%d", &my_array[count])) { count++; } selection_sort(my_array, count); for (i = 0; i < count; i++) { printf("%d\n", my_array[i]); } return EXIT_SUCCESS; */ int my_array[ARRAY_MAX]; clock_t start, end; int i, count = 0; while (count < ARRAY_MAX && 1 == scanf("%d", &my_array[count])) { count++; } start = clock(); selection_sort(my_array, count); end = clock(); for (i = 0; i < count; i++) { printf("%d\n", my_array[i]); } fprintf(stderr, "%d %f\n", count, (end - start) / (double)CLOCKS_PER_SEC); return EXIT_SUCCESS; }
int main() { u_i len = 12; int arr[12] = {2, 13, 12, 16, 15, 4, 17, 8, 1, 18, 14, 9}; int *temp1 = copy(arr, len); insertion_sort(temp1, len); int *temp2 = copy(arr, len); quick_sort(temp2, len); int *temp3 = copy(arr, len); merge_sort(temp3, len); int *temp4 = copy(arr, len); selection_sort(temp4, len); int *temp5 = copy(arr, len); counting_sort(temp5, len); int *temp6 = copy(arr, len); heap_sort(temp6, len); int *temp7 = copy(arr, len); radix_sort(temp7, len); bool eq = equal(temp1, temp2, len) & equal(temp2, temp3, len) & equal(temp3, temp4, len) & equal(temp4, temp5, len) & equal(temp5, temp6, len) & equal(temp6, temp7, len); printf("\x1B[32m""Equal arrays :%s\n""\033[0m", eq ? "true" : "false"); // Free all arrays allocated. }
int main(int argc, char *argv[]) { if (dataset_init(argc, argv) == -1) { dataset_usage(); return EXIT_FAILURE; } int size; int *buf = malloc(SSORT_MAX_NUMS * sizeof(int)); if (buf == NULL) { pexit(progname); } size = dataset_get(buf, SSORT_MAX_NUMS); if (size == -1) { pexit(progname); } selection_sort(buf, size); dataset_print(buf, size); free(buf); return EXIT_SUCCESS; }
int main() { int arr[9] = {9, 3, 2, 7, 6, 1, 4, 5, 8}; selection_sort(arr, 0, 9); print_arr(arr, 9); return 0; }
void main() { int k[N],i; clrscr(); printf("\nEnter Array Elements ...\n\n "); for(i=0;i<N;i++) { printf("Enter k[%d] : ",i); scanf("%d",&k[i]); } clrscr(); printf("\n==========================================="); printf("\nBefore Sorting: "); disp(k); printf("\n===========================================\n\n"); selection_sort(k); printf("\n\n========================================="); printf("\nAfter Sorting: "); disp(k); printf("\n=========================================\n"); getch(); }
//The main function will read a text file, copy the data //int an array and display the time taken to sort the data int main(int argc, char *argv[]){ std::random_device rd; std::mt19937 gen(rd()); int N = 50; std::uniform_real_distribution<> dis(1, 2000); std::vector<double> numbers; std::vector<double> numbers_aux; for (int n=0; n<N; ++n){ numbers.emplace_back(dis(gen)); } numbers_aux = numbers; selection_sort(numbers, N); printData<double>(numbers, "Selection Sort"); numbers = numbers_aux; insertion_sort(numbers, N); printData<double>(numbers, "Insertion Sort"); sort_clock trackTime; trackTime._start = std::chrono::system_clock::now(); numbers = numbers_aux; topdown_merge_sort(numbers, 0, N-1); trackTime._end = std::chrono::system_clock::now(); trackTime._elapsed_seconds = trackTime._end-trackTime._start; std::cout << "Finished Merge Top Sort in:" << trackTime._elapsed_seconds.count() << std::endl; printData<double>(numbers, "Merge Sort"); numbers = numbers_aux; bottomup_merge(numbers, 0, N); printData<double>(numbers, "Merge Sort Bottom Up Sort"); }
void main() { int a[201], n; clrscr(); // se citeste sirul n = read_array(a); // daca a aparut o eroare la citirea datelor se iese din program if (n == -1) { printf("Eroare la citirea datelor!"); getch(); exit(1); } // se apeleaza algoritmul de sortare selection_sort(a, n); printf("\ncomparatii: %d\natribuiri: %d\n\nSirul ordonat: ", comp, atr); // se tipareste sirul sortat print_array(a, n, 1); }
void run_sorts(buf *buf) { fill(buf); while(1) { int r = rand_from(0, 4); fisher_yates_shuffle(buf, 0, buf->length-1); switch (r) { case 0: printf("Insertion Sort.\n"); insertion_sort(buf, 0, buf->length-1); break; case 1: printf("Quick Sort.\n"); quicksort(buf, 0, buf->length-1); break; case 2: printf("Heap Sort.\n"); heap_sort(buf, 0, buf->length-1); break; case 3: printf("Selection Sort.\n"); selection_sort(buf, 0, buf->length-1); break; case 4: printf("Merge Sort.\n"); merge_sort(buf, 0, buf->length-1); break; } } }
void main (int argc, char *argv[]) { int A[] = {5,4,10,38,22,12,50,67,22,1}; print_array(A); selection_sort(A); print_array(A); }
int main(int argc, char* argv[]) { int a[3] = {48,2,24}; selection_sort(a,3); return 0; }
int main() { std::vector<int> v = {8, 2, 3, 4, 7, 1, 5, 9, 10, 6}; v = selection_sort (v); for (auto &i : v) std::cout << i << " "; std::cout << std::endl; }