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

7.QuickSort

The document contains a C++ implementation of the quicksort algorithm using templates. It defines a partition function to rearrange elements and a quicksort function to sort an array. The main function demonstrates sorting an integer array and outputs the sorted result.

Uploaded by

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

7.QuickSort

The document contains a C++ implementation of the quicksort algorithm using templates. It defines a partition function to rearrange elements and a quicksort function to sort an array. The main function demonstrates sorting an integer array and outputs the sorted result.

Uploaded by

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

#include "pch.

h"
#include "iostream"
using namespace System;
using namespace std;

template<typename T>
int particion(T* arr, int p, int r)
{
int x = arr[r];//pivote: ultimo elemento del arreglo
int i = p - 1;//indice de los menores
for (int j = p; j < r; j++)
{
if (arr[j] <= x)
{
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[r]);
return i + 1;
}

template<typename T>
void quicksort(T* arr, int p, int r)
{
int q;

if (p < r)
{
q = particion(arr, p, r);
quicksort(arr, p, q - 1);
quicksort(arr, q + 1, r);
}
}

int main()
{
int arreglo[] = { 4,5,1,9,3,2 };
int n = 6;

quicksort<int>(arreglo, 0, n - 1);
cout << "Arreglo ordenado por quicksort" << endl;
for (int i = 0; i < n; i++)
{
cout << arreglo[i] << " ";
}

cin.ignore();
cin.get();
return 0;
}

You might also like