/******************************************************* sorting.ch Use this program to help students learn how to process data sets using a simple sorting algorithm. This program can also be used to illustrate how sorting might be done automatically by an application such as Excel. ************************************************************ Copyright 2014 SoftIntegration Inc. http://www.softintegration.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 *******************************************************/ // the list that will be sorted int list[6] = {3, 1, 16, 14, 18, 5}; // initialize variables that will be used int i, j; int minimum, temp; // we keep soring until we reach the end of the list // sizeof(list) returns the size of the array in bytes // sizeof(int) returns the size of an int // dividing sizeof(list) by sizeof(int) gives us the lenght // of the array for(i = 0; i < sizeof(list)/sizeof(int); i++) { // start with the first item in the list and advance as we go minimum = i; // find a number smaller than the one stored in minimum for(j = i+1; j < sizeof(list)/sizeof(int); j++) { //if we find a smaller one, store that to minimum if(list[j] < list[minimum]) minimum = j; // swap the new one we found with the number at the front temp = list[i]; list[i] = list[minimum]; list[minimum] = temp; } } for (i = 0; i < sizeof(list)/sizeof(int); i++) { printf("%d\n", list[i]); }