Advertisement
minusa71

Task 7: "selection sort"

Jan 14th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. //task 7: Sorting an array means to arrange its elements in increasing order.
  6. //Write a program to sort an array. Use the "selection sort" algorithm:
  7. //Find the smallest element, move it at the first position, find the smallest from the rest, move it at the second position, etc.
  8.  
  9. namespace Task7Sort
  10. {
  11. class Sort
  12. {
  13. static void Main(string[] args)
  14. {
  15. int[] array ={ 1, 4, 2, 8, 12, 66, -66, 4, 0, 5, -23, 55, 64, 32, 1, 0, -3 };
  16. int[] temp = new int[array.Length];
  17. int min;
  18. for (int i = 0; i < array.Length ; i++)
  19. {
  20. min = i;
  21. for (int j = i + 1; j < array.Length; j++)
  22. {
  23. if (array[j] < array[min])
  24. {
  25. min = j;
  26. }
  27.  
  28. }
  29. if (min != i)
  30. {
  31. temp[min] = array[i];
  32. array[i] = array[min];
  33. array[min] = temp[min];
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. }
  41. foreach (int element in array)
  42. {
  43. Console.Write(element + " ");
  44. }
  45. Console.WriteLine();
  46.  
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement