Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- public class QuickSort {
- ArrayList<Integer> array;
- int leng;
- int mov = 0, comp = 0;
- public static void main(String a[]){
- ArrayList<Integer> arreglo = new ArrayList<Integer>();
- QuickSort p1 = new QuickSort();
- System.out.println("Desordenado ");
- for(int i=0; i<100; i++){
- arreglo.add((int)(Math.random()*200));
- System.out.print(arreglo.get(i)+" ");
- }
- System.out.println();
- p1.array = arreglo;
- p1.leng = arreglo.size();
- p1.quickSort(0, p1.leng - 1);
- System.out.println("Ordenado ");
- for(int i=0; i<100; i++){
- System.out.print(arreglo.get(i)+" ");
- }
- System.out.println("\nComparaciones: "+p1.comp+"\nMovimientos: "+p1.mov);
- }
- private void quickSort(int lowerI, int upperI) {
- int i = lowerI;
- int j = upperI;
- int pivote = array.get(lowerI+(upperI-lowerI)/2);
- while (i <= j) {
- while (array.get(i) < pivote) {
- i++;
- comp++;
- }
- while (array.get(j) > pivote) {
- j--;
- comp++;
- }
- if (i <= j) {
- interNumbers(i, j);
- i++;
- j--;
- }
- }
- if (lowerI < j)
- quickSort(lowerI, j);
- if (i < upperI)
- quickSort(i, upperI);
- }
- public void interNumbers(int i, int j) {
- int temp = array.get(i);
- array.set(i, array.get(j));
- array.set(j, temp);
- mov++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement