BVB Source Codes

Algorithm-Implementations Show selection_sort.c Source code

Return Download Algorithm-Implementations: download selection_sort.c Source code - Download Algorithm-Implementations Source code - Type:.c
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /* Author: Jonathan Lebron 2013
  4.  * Selection Sort:
  5.  *      1. For iteration i, set min index to i
  6.  *      2. Iterate through array
  7.  *      3. If current element is less than element at min index, set
  8.  *      min index to curr index.
  9.  *      4. Exchange element at index i and min index
  10.  */
  11.  
  12. void selectionSort(int a[], int size)
  13. {
  14.     if (size < 2) return;
  15.     int i, j, temp, min_index;
  16.     for (i = 0; i < size; i++){
  17.         min_index = i;
  18.         for (j = i; j < size; j++){
  19.             if (a[j] < a[min_index]) min_index = j;
  20.         }
  21.         if (min_index != i){
  22.             temp = a[i];
  23.             a[i] = a[min_index];
  24.             a[min_index] = temp;
  25.         }
  26.     }
  27. }
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.     if (argc < 2) {
  32.         printf("ERROR: Must provide at least one argument!\n");
  33.         exit(1);
  34.     }
  35.  
  36.     int a[argc-1];
  37.     for (int i = 1; i < argc; i++){
  38.         a[i-1] = atoi(argv[i]);
  39.     }
  40.  
  41.     selectionSort(a, argc-1);
  42.  
  43.     printf("Done sorting... Result is {");
  44.     for (int i = 0; i < argc-1; i++){
  45.         printf("%d, ", a[i]);
  46.     }
  47.     printf("\b\b}\n");
  48. }
  49.  
  50.  
downloadselection_sort.c Source code - Download Algorithm-Implementations Source code
Related Source Codes/Software:
raty - 2017-04-22
RDVTabBarController - Highly customizable tabBar and tabBarController fo... 2017-04-22
material-icon-lib - Library containing over 1500 material vector icons... 2017-04-21
httpdiff - Perform the same request against two HTTP servers ... 2017-04-21
jquerytools - The missing UI library for the Web ... 2017-04-21
mcrouter - Mcrouter is a memcached protocol router for scalin... 2017-04-22
dynomite - A generic dynamo implementation for different k-v ... 2017-04-22
kityminder - Baidu brain figure 2017-04-22
llvm - Mirror of official llvm git repository located at ... 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
ied - 2017-04-29
Nimble - A Matcher Framework for Swift and Objective-C 2017-04-29
MHVideoPhotoGallery - A Photo and Video Gallery 2017-04-29
shoulda-matchers - Collection of testing matchers extracted from Shou... 2017-04-29
Android-SlideExpandableListView - A better ExpandableListView, with animated expanda... 2017-04-29
AppSales-Mobile - App Sales allows iPhone and Mac App Store develope... 2017-04-29
react-templates - Light weight templates for react ... 2017-04-28
afterglow-theme - A minimal dark Theme for Sublime Text 2 and 3 2017-04-28
jwt-go - Golang implementation of JSON Web Tokens (JWT) 2017-04-28
DeerResume - Tool MarkDown online resume, online preview, edit,... 2017-04-28

 Back to top