Bubble Sort

比较相邻两项,条件满足是就交换。

Bubble Sort.mp4

  1. // declare an array of integers that are not sorted
  2. int[] nums = { 5, 10, 3, 2, 4 };
  3. // Output the unsorted array to the console
  4. Console.WriteLine("Before: 5, 10, 3, 2, 4");
  5. // Use this to know when to stop the sorting routine
  6. bool swapped;
  7. // Here we use a do loop but could have used for or while loops as well.
  8. do
  9. {
  10. // set swapped to false so that we can ensure at least one pass on the array
  11. swapped = false;
  12. // This loop will iterate over the array from beginning to end
  13. for (int i = 0; i < nums.Length - 1; i++)
  14. {
  15. // here we use the i for the position in the array
  16. // So i is the first value to compare and i + 1 compares the adjacent value
  17. // Once i is incremented at the end of this loop, we compare the next two sets of values, etc.
  18. if (nums[i] > nums[i + 1])
  19. {
  20. // swap routine. Could be a separate method as well but is used inline for simplicity here
  21. // 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 step
  22. int temp = nums[i + 1];
  23. // Here we replace the right value with the larger value that was on the left. See why we needed the temp variable above?
  24. nums[i + 1] = nums[i];
  25. // Now we assign the value that is in temp, the smaller value, to the location that was just vacated by the larger number
  26. nums[i] = temp;
  27. // Indicate that we did a swap, which means we need to continue to check the remaining values.
  28. swapped = true;
  29. }
  30. }
  31. } while (swapped == true);
  32. // output the sorted array to the console
  33. Console.Write("After: ");
  34. for (int i = 0; i < nums.Length; i++)
  35. {
  36. Console.Write("{0}, ", nums[i]);
  37. }
  38. // Use Console.ReadLine() in the event application was started with debugging.
  39. Console.ReadLine();

Insertion Sort

将当前未排序元素,插入到已排序列的恰当位置。

Insertion Sort.mp4

  1. // Declare an integer array that is not sorted
  2. int[] arr = { 7, 8, 4, 6, 2, 1 };
  3. // Output the values of the array to the console
  4. Console.WriteLine("Array before sort: 7,8,4,6,2,1");
  5. // Declare variable for the insertion value
  6. int newValue;
  7. // The outer loop allows us to iterate over the complete array that we will use for sorting
  8. for (var i = 1; i < arr.Length; i++)
  9. {
  10. // set newValue equal to the second element in the array
  11. // we don't start at the first element because it has no preceding value
  12. // which means we can't move it any further forward in the array
  13. newValue = arr[i];
  14. // Also set the start of our inner loop to the same value as i
  15. var j = i;
  16. // This loop will check the value of j to ensure we have not reached the end of the iteration (j = 0)
  17. // but also checks to see if the value preceding the current value is larger.
  18. // If it is, we "swap" the value before this one, decrement j, and then test against the next value
  19. // This continues until we run out of numbers in the array (j = 0) or there are no more values larger than
  20. // the current number (newValue) to it's left (preceding it).
  21. while (j > 0 && arr[j - 1] > newValue)
  22. {
  23. arr[j] = arr[j - 1];
  24. j--;
  25. }
  26. arr[j] = newValue;
  27. }
  28. Console.Write("Array after sort: ");
  29. foreach (var val in arr)
  30. {
  31. Console.Write(val + ",");
  32. }
  33. Console.WriteLine();

Selection Sort

选出最大(小)的排出来。

Selection Sort.mp4

  1. // Declare an integer array that is not sorted
  2. int[] arr = { 7, 8, 4, 6, 2, 1 };
  3. // Output the values of the array to the console
  4. Console.WriteLine("Array before sort: 7,8,4,6,2,1");
  5. //minPos will keep track of where the minimum value is located
  6. int minPos;
  7. // outer loop will be responsible for ensuring we have iterated over the entire array
  8. for (var i = 0; i < arr.Length - 1; i++)
  9. {
  10. //set minPos to the current counter value for traversing the array
  11. minPos = i;
  12. // inner loop will perform the comparisons between the min and the other values in the array
  13. for (var j = i + 1; j < arr.Length; j++)
  14. {
  15. if (arr[j] < arr[minPos])
  16. {
  17. //minPos will keep track of the index that min is in, this is needed when a swap happens
  18. minPos = j;
  19. }
  20. }
  21. //if minPos no longer equals i, it indicates a smaller value existed so a swap must take place to sort the values
  22. if (minPos != i)
  23. {
  24. var temp = arr[i];
  25. arr[i] = arr[minPos];
  26. arr[minPos] = temp;
  27. }
  28. }
  29. Console.Write("Array after sort: ");
  30. foreach (var val in arr)
  31. {
  32. Console.Write(val + ",");
  33. }
  34. Console.WriteLine();