Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int sizearray = 11;
- int array[] = new int[sizearray];
- int reverse[] = new int[sizearray];
- void setup() {
- noLoop();
- }
- void draw() {
- array = FillArrayRandom(0,20);
- ArrayDisplayer(array);
- quickSort(0, array.length-1);
- ArrayDisplayer(array);
- reverse = reverseSort(array);
- ArrayDisplayer(reverse);
- }
- public int[] FillArrayRandom(int minRandom, int MaxRandom) {
- for (int i = 0; i < sizearray; i++) {
- array[i] = (int(random(minRandom, MaxRandom)));
- }
- return array;
- }
- void ArrayDisplayer(int[] array) {
- print("Array size=" + array.length + " [ ");
- for (int i = 0; i < array.length; i++) {
- if (array.length-1 == i) { print(i+": " + array[i] + " "); }
- else { print(i+": " + array[i] + ", "); }
- }
- println("]");
- }
- /*
- public int MinValue(int[] array) {
- int MinV = array[0];
- for (int i = 1; i <= array.length-1; i++) {
- if (array[i] < MinV) { MinV = array[i]; }
- }
- return MinV;
- }
- public int MaxValue(int[] array) {
- int MaxV = array[0];
- for (int i = 1; i <= array.length-1; i++) {
- if (array[i] > MaxV) { MaxV = array[i]; }
- }
- return MaxV;
- }
- */
- /*public int[] quickSort(int[] array, int minIndex, int maxIndex) {
- int switcher = array[0+(array.length-1-0)/2];
- while (minIndex <= maxIndex) {
- while (array[minIndex] < switcher) {
- minIndex++;
- }
- while (array[maxIndex] > switcher) {
- maxIndex--;
- }
- if (minIndex <= maxIndex) {
- SwapNumber(
- }
- if (minIndex
- return array;
- }
- void SwapNumber(int a, int b) {
- int temp = array[a];
- array[a] = array[b];
- array[b] = temp;
- }*/
- void quickSort(int lowerIndex, int higherIndex) {
- int i = lowerIndex;
- int j = higherIndex;
- int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
- while (i <= j) {
- while (array[i] < pivot) {
- i++;
- }
- while (array[j] > pivot) {
- j--;
- }
- if (i <= j) {
- exchangeNumbers(i, j);
- i++;
- j--;
- }
- }
- if (lowerIndex < j)
- quickSort(lowerIndex, j);
- if (i < higherIndex)
- quickSort(i, higherIndex);
- }
- void exchangeNumbers(int i, int j) {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- public int[] reverseSort(int[] array) {
- int reversed[] = new int[array.length];
- int count = 0;
- int baselength = array.length-1;
- for (int i = baselength; i >= 0; i--) {
- reversed[count] = array[i];
- count++;
- }
- return reversed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement