Last active
October 18, 2021 09:30
-
-
Save Zelakolase/e68a8861e287fbea59836c6ca197640e to your computer and use it in GitHub Desktop.
A Sorting Method using Randomization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class main { | |
public static void main(String[] args) { | |
int[] a = new int[] {250,93,105,314,7,80,92}; | |
int REPEAT = (int) Math.round(Math.pow(a.length,3.0103821378)); | |
int REAL_LENGTH = a.length - 2; | |
int i = 0; | |
while(i < REPEAT) { | |
int DaNumber = RandomNumber(0,REAL_LENGTH+1); | |
if(a[DaNumber]>a[DaNumber+1]) { | |
int tmp = a[DaNumber+1]; | |
a[DaNumber+1] = a[DaNumber]; | |
a[DaNumber] = tmp; | |
} | |
print(a); | |
i++; | |
} | |
} | |
public static int RandomNumber(int min, int max) { | |
return (int) ((Math.random() * (max - min)) + min); | |
} | |
public static void print(int[] h) { | |
for (int element: h) { | |
System.out.print(element+","); | |
} | |
System.out.println(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment