0% found this document useful (0 votes)
47 views2 pages

Estructura de Datos: Matrix Largom

This document contains code for implementing a quicksort algorithm in Java. The code defines a class with methods for sorting an integer array using quicksort, including a sort method that calls a recursive quicksort method. The quicksort method selects a pivot element and partitions the array around that pivot, recursively calling itself on the subarrays until the entire array is sorted. The code also includes a main method that takes user input for array size and elements, calls the sort method, and prints the original and sorted arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

Estructura de Datos: Matrix Largom

This document contains code for implementing a quicksort algorithm in Java. The code defines a class with methods for sorting an integer array using quicksort, including a sort method that calls a recursive quicksort method. The quicksort method selects a pivot element and partitions the array around that pivot, recursively calling itself on the subarrays until the entire array is sorted. The code also includes a main method that takes user input for array size and elements, calls the sort method, and prints the original and sorted arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ESTRUCTURA DE DATOS

Cdigo Quicksort
import java.util.Scanner;
public class Ordenacion_quicksort {
private int matrix[];
private int largom;
public void sort(int[] inmatrix) {
if (inmatrix == null || inmatrix.length == 0) {
return;
}
this.matrix = inmatrix;
largom = inmatrix.length;
quickSort(0, largom - 1);
}
private void quickSort(int indiceL, int indiceH) {
int i = indiceL;
int j = indiceH;
int pivot = matrix[indiceL+(indiceH-indiceL)/2];
while (i <= j) {
while (matrix[i] < pivot) {
i++;
}
while (matrix[j] > pivot) {
j--;
}
if (i <= j) {
cambio(i, j);
i++;
j--;
for (int k = 0; k < matrix.length; k++) {
System.out.print(matrix[k]+" ,");
}
System.out.println("");
}
}
if (indiceL < j)
quickSort(indiceL, j);
if (i < indiceH)
quickSort(i, indiceH);
}
private void cambio(int i, int j) {
int temp = matrix[i];
matrix[i] = matrix[j];
matrix[j] = temp;
}
public static void main(String a[]){

Ordenacion_quicksort sorter = new Ordenacion_quicksort();


int lim;
int [] vector;
Scanner teclado = new Scanner (System.in);
System.out.println("Ordenamiento QuickSort");
System.out.println("Ingrese el limite");
lim=teclado.nextInt();
vector=new int [lim];
for (int i = 0; i < lim ; i++) {
System.out.print("x["+i+"]=");
vector[i]= teclado.nextInt();
}
System.out.println("\n\nEl vector original es: ");
for (int i = 0; i < lim; i++) {
System.out.print(vector[i]+ ", ");
}
System.out.println("\n\nORDENACION CON PASADAS");
sorter.sort(vector);
System.out.println("\nEl vector ordenado es: ");
for(int i:vector){
System.out.print(i);
System.out.print(" ");
}
}
}

You might also like