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

Quick Sort 3

This document contains code for implementing a quicksort algorithm in C#. It defines a main method that gets the size of an array from the user and initializes a class to sort the array. The class constructor gets user input to populate the array, calls the quicksort method to sort it, and displays the sorted array. The quicksort method recursively partitions the array around a pivot element and sorts the subarrays.
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)
25 views2 pages

Quick Sort 3

This document contains code for implementing a quicksort algorithm in C#. It defines a main method that gets the size of an array from the user and initializes a class to sort the array. The class constructor gets user input to populate the array, calls the quicksort method to sort it, and displays the sorted array. The quicksort method recursively partitions the array around a pivot element and sorts the subarrays.
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/ 2

Quick sort 3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace quicksort
{
class Class
{
static void Main()
{
int n;
Console.WriteLine("Metodo de Quick Sort");
Console.Write("Cuantos longitud del vector: ");
n = Int32.Parse(Console.ReadLine());
llenar b = new llenar(n);
}
}
class llenar
{
int h;
int[] vector;
public llenar(int n)
{
h = n;
vector = new int[h];
for (int i = 0; i < h; i++)
{
Console.Write("ingrese valor {0}: ", i + 1);
vector[i] = Int32.Parse(Console.ReadLine());
}
quicksort(vector, 0, h - 1);
mostrar();
}
private void quicksort(int[] vector, int primero, int ultimo)
{
int i, j, central;
double pivote;
central = (primero + ultimo) / 2;
pivote = vector[central];
i = primero;
j = ultimo;
do

{
while (vector[i] < pivote) i++;
while (vector[j] > pivote) j--;
if (i <= j)
{
int temp;
temp = vector[i];
vector[i] = vector[j];
vector[j] = temp;
i++;
j--;
}
} while (i <= j);
if (primero < j)
{
quicksort(vector, primero, j);
}
if (i < ultimo)
{
quicksort(vector, i, ultimo);
}
}
private void mostrar()
{
Console.WriteLine("Vector ordenados en forma ascendente");
for (int i = 0; i < h; i++)
{
Console.Write("{0} ", vector[i]);
}
Console.ReadLine();
}
}
}

You might also like