0% found this document useful (0 votes)
18 views

Quick Sort

Uploaded by

Duke Ero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Quick Sort

Uploaded by

Duke Ero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Quicksort

procedimento QuickSort(X[], IniVet, FimVet)


var
i, j, pivo, aux
início
i <- IniVet
j <- FimVet
pivo <- X[(IniVet + FimVet) div 2]
enquanto(i <= j)
| enquanto (X[i] < pivo) faça
| | i <- i + 1
| fimEnquanto
| enquanto (X[j] > pivo) faça
| | j <- j - 1
| fimEnquanto
| se (i <= j) então
| | aux <- X[i]
| | X[i] <- X[j]
| | X[j] <- aux
| | i <- i + 1
| | j <- j - 1
| fimSe
fimEnquanto
se (IniVet < j) então
| QuickSort(X, IniVet, j)
fimSe
se (i < FimVet) então
| QuickSort(X, i, FimVet)
fimSe
fimProcedimento
import java.util.Arrays;

class Main

public static void swap (int[] arr, int i, int j)

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

// Partition using the Lomuto partition scheme

public static int partition(int[] a, int start, int end)

// Pick the rightmost element as a pivot from the array

int pivot = a[end];

// elements less than the pivot will be pushed to the left of `pIndex`

// elements more than the pivot will be pushed to the right of `pIndex`

// equal elements can go either way

int pIndex = start;

// each time we find an element less than or equal to the pivot,

// `pIndex` is incremented, and that element would be placed

// before the pivot.

for (int i = start; i < end; i++)

if (a[i] <= pivot)

swap(a, i, pIndex);

pIndex++;
}

// swap `pIndex` with pivot

swap(a, end, pIndex);

// return `pIndex` (index of the pivot element)

return pIndex;

// Quicksort routine

public static void quicksort(int[] a, int start, int end)

// base condition

if (start >= end) {

return;

// rearrange elements across pivot

int pivot = partition(a, start, end);

// recur on subarray containing elements less than the pivot

quicksort(a, start, pivot - 1);

// recur on subarray containing elements more than the pivot

quicksort(a, pivot + 1, end);

// Java implementation of the Quicksort algorithm

public static void main(String[] args)

{
int[] a = { 9, -3, 5, 2, 6, 8, -6, 1, 3 };

quicksort(a, 0, a.length - 1);

// print the sorted array

System.out.println(Arrays.toString(a));

}
/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication8;

import java.util.*;

import java.io.*;

import java.nio.*;

public class JavaApplication8 {

public static void LeTexto()

/* Scanner in = new Scanner(new FileReader("dados.txt"));

while (in.hasNextLine()) {

String line = in.nextLine();

System.out.println(line);

}*/

File arquivo = new File("/home/hallan/nome_do_arquivo.txt");

try

if (!arquivo.exists())

//cria um arquivo (vazio)


arquivo.createNewFile();

//caso seja um diretório, é possível listar seus arquivos e diretórios

File[] arquivos = arquivo.listFiles();

//escreve no arquivo

FileWriter fw = new FileWriter(arquivo, true);

BufferedWriter bw = new BufferedWriter(fw);

bw.write("Texto a ser escrito no txt");

bw.newLine();

bw.close();

fw.close();

//faz a leitura do arquivo

FileReader fr = new FileReader(arquivo);

BufferedReader br = new BufferedReader(fr);

//enquanto houver mais linhas

while (br.ready())

//lê a proxima linha

String linha = br.readLine();

//faz algo com a linha

System.out.println(linha);

br.close();
fr.close();

catch (IOException ex)

ex.printStackTrace();

public static int[][] create2DIntMatrixFromFile(String filename) throws Exception

int[][] matrix = {{1}, {2}};

File inFile = new File(filename);

Scanner in = new Scanner(inFile);

in.useDelimiter("[/n]");

String line = "";

int lineCount = 0;

while (in.hasNextLine())

line = in.nextLine().trim();

Scanner lineIn = new Scanner(line);

lineIn.useDelimiter("");

for (int i = 0; lineIn.hasNext(); i++) {

matrix[lineCount][i] = Integer.parseInt(lineIn.next());

lineIn.next();

lineCount++;
}

return matrix;

public static void main(String[] args) throws Exception

// TODO code application logic here C:\c

int [][] Mat1 = create2DIntMatrixFromFile("C:\\c\\a.txt");

for (int i=0; i< Mat1.length; i++)

for (int j=0; i< Mat1[i].length; j++)

System.out.print(Mat1[i][j]);

System.out.print(" ");

System.out.println("");

}
package ordenacao_vetores;

import java.util.*;

public class Ordenacao_vetores {

private static void Quicksort(int values[], int began, int end)

int i, j, pivo, aux;

i = began;

j = end-1;

pivo = values[(began + end) / 2];

while(i <= j)

while(values[i] < pivo && i < end)

i++;

while(values[j] > pivo && j > began)

j--;

if(i <= j)

aux = values[i];

values[i] = values[j];

values[j] = aux;

i++;

j--;

}
}

if(j > began)

Quicksort(values, began, j+1);

if(i < end)

Quicksort(values, i, end);

public static void main(String[] args) {

int [] array = {5, 8, 1, 2, 7, 3, 6, 9, 4, 10};

Quicksort(array, 0, 10);

for(int i = 0; i < 10; i++)

System.out.print(array[i] + " ");

String nome;

Scanner le = new Scanner (System.in);

nome = le.nextLine();

System.out.println(nome);

You might also like