快速排序

#include <stdio.h>
void quick_sort(int array[], int left, int right)
{
	int i = left, j = right;
	int temp;
	int pivot;

	pivot = array[(left + right) / 2];

	while (i <= j)
	{
		while (array[i] < pivot)
		{
			i++;
		}
		while (array[j] > pivot)
		{
			j--;
		}
		if (i <= j)
		{
			temp = array[i];
			array[i] = array[j];
			array[j] = temp;
			i++;
			j--;
		}

	}

	if (left < j)
	{
		quick_sort(array, left, j);
	}

	if (i < right)
	{
		quick_sort(array, i, right);
	}
}

int main(void)
{
	int array[] = {73, 108, 111, 118, 101, 70, 105, 115, 104, 67, 46, 99, 111, 109};
	int i, length;

	length = sizeof(array) / sizeof(array[0]);
	quick_sort(array, 0, length-1);
	for (i = 0; i < length; i++)
	{
		printf("%d ", array[i]);
	}
	putchar('\n');

	return 0;
}
//快速排序
#include<iostream>
using namespace std;
void quickSort(int array[],int left,int right){
    int pivot=array[(left+right)/2];
    int i=left,j=right;
    while(array[i]<pivot){
        i++;
    }
    while(array[j]>pivot){
        j--;
    }
    
if (i <= j)
        {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            i++;
            j--;
        }
    if (left < j)
    {
        quickSort(array, left, j);
    }

    if (i < right)
    {
        quickSort(array, i, right);
    }

}
int main(void)
{
    int array[] = {73, 108, 111, 118, 101, 70, 105, 115, 104, 67, 46, 99, 111, 109};
    int i, length;

    length = sizeof(array) / sizeof(array[0]);
    quickSort(array, 0, length-1);
    for (i = 0; i < length; i++)
    {
        printf("%d ", array[i]);
    }
    putchar('\n');

    return 0;
}
View Code

 

posted @ 2019-07-09 22:18  Hello_World2020  阅读(156)  评论(0编辑  收藏  举报