快速排序

快速排序

快速排序:

通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分关键字小,则分别对这两部分继续进行排序,直到整个序列有序。

把整个序列看做一个数组,把第零个位置看做中轴,和最后一个比,如果比它小交换,比它大不做任何处理; 交换了以后再和小的那端比,比它小不交换,比他大交换。

这样循环往复,一趟排序完成,左边就是比中轴小的,右边就是比中轴大的, 然后再用分治法,分别对这两个独立的数组进行排序。

 

快速排序是对冒泡排序的一种本质改进。

不稳定,时间复杂度 最理想 O(n) 最差时间O(n^2)   平均复杂度:O(NlogN)



 

#include <iostream.h>  
#include <stdio.h>  
  
using namespace std;  
  
int partition(int data[], int low, int high);  
void sort(int data[], int low, int high);  
void change(int data[], int low, int high);  
  
int partition(int data[], int low, int high)  
{  
    int i= low, j = high;  
    int pivotKey = data[low];  
    while(i<j){  
        while(i<j && data[j]>=pivotKey){  
            j--;  
        }  
        if(i<j){  
            data[i] = data[j];  
            i++;  
        }  
        while(i<j && data[i] < pivotKey){  
            i++;  
        }  
        if(i<j){  
            data[j] = data[i];  
            j--;  
        }  
    }  
    data[i] = pivotKey;  
    return i;  
}  
  
void change(int data[], int low, int high)  
{  
    int temp;  
    temp = data[high];  
    data[high] = data[low];  
    data[low] = temp;  
}  
  
void sort(int data[], int low, int high)  
{  
    if(low < high)  
    {  
        int pivot = partition(data, low, high);  
        sort(data, low, pivot-1);  
        sort(data, pivot+1, high);  
    }  
}  
  
int main()  
{  
    int data[10] = {32,43,54,21,43,46,67,34,48,56};  
    for(int i=0; i<10; i++)  
    {     
        cout<<"data["<<i<<"]="<<data[i]<<endl;  
    }  
    sort(data, 0, 9);  
    for(int i=0; i<10; i++)  
    {     
        cout<<data[i]<<"  "<<endl;  
    }  
}  
 
    1. public class QuickSort {  
    2.     /** 
    3.      * 获取中轴位置 
    4.      * @param list 
    5.      * @param low 
    6.      * @param high 
    7.      * @return 
    8.      */  
    9.     public int getMiddle(Integer[] list, int low, int high) {  
    10.           
    11.         // 数组的第一个作为中轴  
    12.         int tmp = list[low];  
    13.         while (low < high) {  
    14.             while (low < high && list[high] > tmp) {  
    15.                 high--;  
    16.             }  
    17.               
    18.             // 比中轴小的记录移到低端  
    19.             list[low] = list[high];  
    20.             while (low < high && list[low] < tmp) {  
    21.                 low++;  
    22.             }  
    23.               
    24.             // 比中轴大的记录移到高端  
    25.             list[high] = list[low];  
    26.         }  
    27.           
    28.         // 中轴记录到位  
    29.         list[low] = tmp;  
    30.           
    31.         // 返回中轴的位置  
    32.         return low;   
    33.     }  
    34.   
    35.     public void _quickSort(Integer[] list, int low, int high) {  
    36.         if (low < high) {  
    37.               
    38.             // 将list数组进行一分为二  
    39.             int middle = getMiddle(list, low, high);  
    40.               
    41.              // 对低字表进行递归排序  
    42.             _quickSort(list, low, middle - 1);  
    43.               
    44.              // 对高字表进行递归排序  
    45.             _quickSort(list, middle + 1, high);  
    46.         }  
    47.     }  
    48.   
    49.     public void quick(Integer[] str) {  
    50.         if (str.length > 0) {   
    51.             // 查看数组是否为空  
    52.             _quickSort(str, 0, str.length - 1);  
    53.         }  
    54.     }  
    55.   
    56.     public static void main(String[] args) {  
    57.         Integer[] list = { 3435322371410 };  
    58.         QuickSort qs = new QuickSort();  
    59.         qs.quick(list);  
    60.         for (int i = 0; i < list.length; i++) {  
    61.             System.out.print(list[i] + " ");  
    62.         }  
    63.         System.out.println();  
    64.     }  
    65.   
    66. }  

6.冒泡排序

#include <iostream>
using namespace std;
void swap(int *left ,int *right)
{
    int temp=*left;
    *left=*right;
    *right=temp;
}

void BubbleSort2(int arr[],int num)
{
    int k=num;
    int j;
    bool flag=true;
    while(flag)
    {
        flag=false;
        for(j=1;j<k;j++)
        {
            if(arr[j-1]>arr[j])
            {
                swap(&arr[j-1],&arr[j]);
                flag=true;
            }
        }
        k--;
    }
}


7.二叉树中序输出

原文地址:https://www.cnblogs.com/beipiaoboy/p/3256241.html