Advertisement
Guest User

Untitled

a guest
Jan 10th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //int[] tomb = new int[3] { 1, 2, 3 }; // 0 csere
  2. //int[] tomb = new int[3] { 3, 2, 1 };  // 1 csere
  3. //int[] tomb = new int[3] { 2, 3, 1 };  // 2 csere
  4. //int[] tomb = new int[4] { 2, 4, 3, 1 };  // 2 csere
  5. //int[] tomb = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};    // 0 csere
  6. //int[] tomb = new int[10] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};    // 5 csere
  7. //int[] tomb = new int[10] { 1, 2, 3, 4, 5, 6, 7, 9, 8, 10 };   // 1 csere
  8. //int[] tomb = new int[10] { 1, 7, 10, 3, 2, 8, 4, 9, 6, 5 };   // 7 csere
  9.  
  10. int[] tomb = new int[10] { 7, 10, 1, 3, 2, 8, 4, 9, 6, 5 };     // 7 csere
  11.  
  12. int csere = 0;
  13.  
  14. for (int i = 0; i < tomb.Length; i++) {
  15.     Console.Write(tomb[i]+" ");
  16. }
  17.  
  18. for (int i = 0; i < tomb.Length; i++) {
  19.     if (tomb[i] != i+1) {
  20.         int j = i;
  21.         int tmp = tomb[j];
  22.         tomb[i] = i+1;
  23.         do {
  24.             j++;
  25.             if (tomb[j] == i+1) { break; }
  26.            
  27.         } while (true);
  28.         tomb[j] = tmp;
  29.  
  30.         csere++;
  31.     }
  32. }
  33.  
  34. Console.WriteLine("\nCserek szama: " + csere );
  35.  
  36. for (int i = 0; i < tomb.Length; i++){
  37.     Console.Write(tomb[i] + " ");
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement