Advertisement
Guest User

Sorting

a guest
Jun 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. int[] myArray = {8, 5, 10, 5, 2, 9, 7, 0, 7, 10, 2, 7, 9, 3, 9, 7, 5, 10, 0, 9, 3, 5, 4, 8};
  9. int[] myArray1 = {15, 99, 21, 27, 33, 34, 65, 75, 58, 85, 69, 49};
  10. int[] myArray2 = {66, 22, 11, 11, 11, 11, 11, 0, 1, 5, 6, 7, 8, 1, 2, 3, 3, 2, 11};
  11. System.out.println("My array = " + Arrays.toString(myArray));
  12. thirdSortingArray(myArray);
  13. System.out.println("My new array = " + Arrays.toString(myArray) + "\n");
  14. System.out.println("My array1 = " + Arrays.toString(myArray1));
  15. secondSortingArray(myArray1);
  16. System.out.println("My new array1 = " + Arrays.toString(myArray1) + "\n");
  17. System.out.println("My array2 = " + Arrays.toString(myArray2));
  18. sortingArary(myArray2);
  19. System.out.println("My new array2 = " + Arrays.toString(myArray2) + "\n");
  20. }
  21.  
  22. public static void sortingArary(int[] myArray) {
  23. for (int i = 0; i < myArray.length; i++) {
  24. for (int j = 0; j < myArray.length - 1; j++) {
  25. if (myArray[j] > myArray[j + 1]) {
  26. int temp = myArray[j];
  27. myArray[j] = myArray[j + 1];
  28. myArray[j + 1] = temp;
  29. }
  30. }
  31. }
  32. }
  33.  
  34. public static void secondSortingArray(int[] myArray) {
  35. for (int j = 0; j < myArray.length; j++) {
  36. int temp = j;
  37. for (int i = j; i < myArray.length; i++) {
  38. if (myArray[i] < myArray[temp]) {
  39. temp = i;
  40. }
  41. }
  42. int temp2 = myArray[j];
  43. myArray[j] = myArray[temp];
  44. myArray[temp] = temp2;
  45. // System.out.println(Arrays.toString(myArray));
  46. }
  47. }
  48.  
  49. public static void thirdSortingArray(int[] myArray) {
  50. for (int i = 1; i < myArray.length; i++) {
  51. for (int j = i; j > 0; j--) {
  52. if (myArray[j - 1] > myArray[j]) {
  53. int temp = myArray[j];
  54. myArray[j] = myArray[j - 1];
  55. myArray[j - 1] = temp;
  56. }
  57. }
  58. // System.out.println(Arrays.toString(myArray));
  59. }
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement