Bubble Sort
比较相邻两项,条件满足是就交换。
// declare an array of integers that are not sortedint[] nums = { 5, 10, 3, 2, 4 };// Output the unsorted array to the consoleConsole.WriteLine("Before: 5, 10, 3, 2, 4");// Use this to know when to stop the sorting routinebool swapped;// Here we use a do loop but could have used for or while loops as well.do{// set swapped to false so that we can ensure at least one pass on the arrayswapped = false;// This loop will iterate over the array from beginning to endfor (int i = 0; i < nums.Length - 1; i++){// here we use the i for the position in the array// So i is the first value to compare and i + 1 compares the adjacent value// Once i is incremented at the end of this loop, we compare the next two sets of values, etc.if (nums[i] > nums[i + 1]){// swap routine. Could be a separate method as well but is used inline for simplicity here// temp is used to hold the right value in the comparison so we don't lose it. That value will be replaced in the next stepint temp = nums[i + 1];// Here we replace the right value with the larger value that was on the left. See why we needed the temp variable above?nums[i + 1] = nums[i];// Now we assign the value that is in temp, the smaller value, to the location that was just vacated by the larger numbernums[i] = temp;// Indicate that we did a swap, which means we need to continue to check the remaining values.swapped = true;}}} while (swapped == true);// output the sorted array to the consoleConsole.Write("After: ");for (int i = 0; i < nums.Length; i++){Console.Write("{0}, ", nums[i]);}// Use Console.ReadLine() in the event application was started with debugging.Console.ReadLine();
Insertion Sort
将当前未排序元素,插入到已排序列的恰当位置。
// Declare an integer array that is not sortedint[] arr = { 7, 8, 4, 6, 2, 1 };// Output the values of the array to the consoleConsole.WriteLine("Array before sort: 7,8,4,6,2,1");// Declare variable for the insertion valueint newValue;// The outer loop allows us to iterate over the complete array that we will use for sortingfor (var i = 1; i < arr.Length; i++){// set newValue equal to the second element in the array// we don't start at the first element because it has no preceding value// which means we can't move it any further forward in the arraynewValue = arr[i];// Also set the start of our inner loop to the same value as ivar j = i;// This loop will check the value of j to ensure we have not reached the end of the iteration (j = 0)// but also checks to see if the value preceding the current value is larger.// If it is, we "swap" the value before this one, decrement j, and then test against the next value// This continues until we run out of numbers in the array (j = 0) or there are no more values larger than// the current number (newValue) to it's left (preceding it).while (j > 0 && arr[j - 1] > newValue){arr[j] = arr[j - 1];j--;}arr[j] = newValue;}Console.Write("Array after sort: ");foreach (var val in arr){Console.Write(val + ",");}Console.WriteLine();
Selection Sort
选出最大(小)的排出来。
// Declare an integer array that is not sortedint[] arr = { 7, 8, 4, 6, 2, 1 };// Output the values of the array to the consoleConsole.WriteLine("Array before sort: 7,8,4,6,2,1");//minPos will keep track of where the minimum value is locatedint minPos;// outer loop will be responsible for ensuring we have iterated over the entire arrayfor (var i = 0; i < arr.Length - 1; i++){//set minPos to the current counter value for traversing the arrayminPos = i;// inner loop will perform the comparisons between the min and the other values in the arrayfor (var j = i + 1; j < arr.Length; j++){if (arr[j] < arr[minPos]){//minPos will keep track of the index that min is in, this is needed when a swap happensminPos = j;}}//if minPos no longer equals i, it indicates a smaller value existed so a swap must take place to sort the valuesif (minPos != i){var temp = arr[i];arr[i] = arr[minPos];arr[minPos] = temp;}}Console.Write("Array after sort: ");foreach (var val in arr){Console.Write(val + ",");}Console.WriteLine();
