对数组[11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22]进行快速排序,输出排序后的数组。
进阶:时间复杂度,空间复杂度
#include<iostream> #include<vector> using namespace std; int Partition(std::vector<int>& L, int low, int high) { int pivotkey = L[low]; while (low < high) { while (low<high && L[high]>=pivotkey) --high; L[low] = L[high]; while (low<high && L[low]<=pivotkey) ++low; L[high] = L[low]; } L[low] = pivotkey; return low; } void QSort(std::vector<int>& L, int low, int high) { if (low < high) { int pivotloc = Partition(L, low, high); QSort(L, low, pivotloc-1); QSort(L, pivotloc+1, high); } } int main(){ vector<int> m={11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22}; QSort(m,0,13); for(int i=0;i<m.size();i++){ cout<<m[i]<<endl; } return 0; }
#include<cstdio> #include<cstdlib> #include<ctime> void swap(int *arr, int i, int j) { if (i == j) return; arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; } void partition(int *arr, int left, int right, int *help) { srand((unsigned int)time(NULL)); int ind = left + (int)((double)rand() / RAND_MAX * (right - left)); swap(arr, ind, right); int less = left - 1, more = right; while (left < more) { if (arr[left] < arr[right]) swap(arr, ++less, left++); else if (arr[left] == arr[right]) ++left; else swap(arr, --more, left); } swap(arr, more, right); help[0] = less + 1; help[1] = more; } void quickSort(int *arr, int left, int right) { if (left >= right) return; int help[2]; partition(arr, left, right, help); quickSort(arr, left, help[0] - 1); quickSort(arr, help[1] + 1, right); } int main() { int arr[] = {11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22}; int n = sizeof(arr) / sizeof(int); quickSort(arr, 0, n - 1); for (int i = 0; i < n; ++i) printf("%d\n", arr[i]); }
//java版本: public class Main{ public static void main(String[] args){ //建立数组 int[] list = new int[]{11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22}; //快速排序 quickSort(list,0,list.length-1); //打印 for(int i : list){ System.out.println(i); } } public static void quickSort(int[] list , int start, int end){ //如果数组大小小于等于1则直接返回 if(start>=end)return; //获取中间值(第一个),设置左右指针 int midval = list[start]; int left = start; int right = end; while(left<right){ //右指针向左遍历,如果值大于中间值则略过 while(left<right&&list[right]>midval){ right--; } //左指针向右遍历,如果值小于中间值则略过 while(left<right&&list[left]<=midval){ left++; } //如果左右两边都停在了“不合群”的数字上,则将其交换 if(left<right){ int tem = list[left]; list[left] = list[right]; list[right] = tem; } } //将中间值换到它该在的地方 list[start] = list[right]; list[right] = midval; //将中间值左右两边再拿去排序 quickSort(list,start,right-1); quickSort(list,right+1,end); } }
def quickpri(a, start, end): pivot = start j = start + 1 for i in range(start + 1, end + 1): if a[i] <= a[pivot]: a[i], a[j] = a[j], a[i] j += 1 a[pivot], a[j - 1] = a[j - 1], a[pivot] pivot = j - 1 #print(a[pivot], a[start:pivot], a[pivot + 1 : end + 1]) return pivot def quicksort(a, start, end): if start >= end: return pivot = quickpri(a, start, end) quicksort(a, start, pivot - 1) quicksort(a, pivot + 1, end) a = [11, 99, 33, 69, 77, 88, 55, 11, 33, 36, 39, 66, 44, 22] quicksort(a, 0, 13) for i in range(0,len(a)): print(a[i])
def quick_sort(array): # 如果数组为空或只有一个元素,直接返回 if len(array) <= 1: return array # 选择第一个元素作为基准 pivot = array[0] # 创建两个空列表,用于存储小于或等于基准和大于基准的元素 left = [] right = [] # 遍历除了基准以外的元素,根据大小放入相应的列表 for i in range(1, len(array)): if array[i] <= pivot: left.append(array[i]) else: right.append(array[i]) # 对左右两个列表递归地进行快速排序,并将结果拼接起来 return quick_sort(left) + [pivot] + quick_sort(right) # 测试 array = [11, 99, 33, 69, 77, 88, 55, 11, 33, 36, 39, 66, 44, 22] sorted_array = quick_sort(array) for i in sorted_array: print(i) # print(sorted_array)
l=[11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22] l.sort() for i in l: print(i)
#include <stdio.h> int input[14]={11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22}; void swap(int* ARR, int low, int high){ int temp = ARR[high]; ARR[high] = ARR[low]; ARR[low] = temp; } int partition(int* ARR, int low, int high){ int m, pivotkey; m = low + (high - low)/2; if(ARR[high]<ARR[low]){ swap(ARR,low,high); } if(ARR[m]>ARR[high]){ swap(ARR,m,high); } if(ARR[m]>ARR[low]){ swap(ARR,m,low); } pivotkey = ARR[low]; while(low < high){ while(low < high && ARR[high] >= pivotkey){ high --; } swap(ARR, low, high); while(low < high && ARR[low] <= pivotkey){ low ++; } swap(ARR, low, high); } return low; } void Qsort(int* ARR, int low, int high){ int pivot; while(low < high){ pivot = partition(ARR, low, high); Qsort(ARR, low, pivot - 1); low = pivot + 1; } } int main(){ Qsort(input, 0, 13); for (int i = 0; i < 14; i++){ printf("%d\n",input[i]); } return 0; }
import java.util.* /** * Author: Damiao * Version: 1.1 * Date:2021/8/12 * Mender: * Modify: * Description: 快速排序 * 链接:https://www.nowcoder.com/questionTerminal/de246eaea4cf4d90bf39b0a0a149dd1d 来源:牛客网 对数组[11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22]进行快速排序,输出排序后的数组。 输入描述: 无 输出描述: 打印出快速排序后的数组,每个数字之间用换行隔开,例如: 11 22 33 44 */ class QuickSort { companion object { @JvmStatic fun quickSort(a: IntArray, left: Int, right: Int) { if (left < right) { val part = partition(a, left, right) quickSort(a, left, part[0] - 1) quickSort(a, part[1] + 1, right) } } /** * 切分 * * @param a * @param left * @param right */ fun partition(a: IntArray, left: Int, right: Int): IntArray { var less = left - 1 // 小于区域边界 var more = right // 大于区域边界 var cur = left // 指向左侧索引,用于数组遍历移动 // 当数组遍历到右侧大于区域边界时,结束遍历 while (cur < more) { // 小于最右侧的值,则执行交换,同时cur指针++,左侧小于边界++ if (a[cur] < a[right]) { swap(a, cur++, ++less) } else if (a[cur] > a[right]) { // 大于最右侧的值,同样执行交换,将大于边界的前一位交换到cur位置,cur位置不变,右侧边界扩大 swap(a, cur, --more) } else { // 等于时,则只移动指针 cur++ } } swap(a, more, right) return intArrayOf(less + 1, more) } fun swap(array: IntArray, i: Int, j: Int) { val temp = array[i] array[i] = array[j] array[j] = temp } } } fun main(args: Array<String>) { val array = intArrayOf(11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22) QuickSort.quickSort(array, 0, array.size - 1) array.forEach { println(it) } }
#include <iostream> using namespace std; int A[14]={11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22}; void quick_sort(int A[],int l,int r){ if(l>=r) return; int i=l-1;int j=r+1; while(i<j){ do ++i;while(A[i]<A[(l+r)/2]); do --j;while(A[j]>A[(l+r)/2]); if(i<j){ int temp=A[i]; A[i]=A[j]; A[j]=temp; } } quick_sort(A, l, j); quick_sort(A, j+1, r); } int main(){ quick_sort(A, 0, 13); for(int i=0;i<14;i++){ printf("%d\n",A[i]); } return 0; }
def partition(A,p,r): x=A[r] i=p-1 for j in range(p,r): if A[j]<=x: i=i+1 A[i],A[j]=A[j],A[i] A[i+1],A[r]=A[r],A[i+1] return i+1 def qSort(A,p,r): if p<r: q=partition(A,p,r) qSort(A, p, q-1) qSort(A,q+1,r) A=[11, 99, 33 , 69, 77, 88, 55, 11, 33, 36,39, 66, 44, 22] qSort(A, 0, len(A)-1) for k in range(len(A)): print(A[k])
#include "bits/stdc++.h" using namespace std; void quickSort(int *num,int len); int main(){ int num[]={11,99,33,69,77,88,55,11,33,36,39,66,44,22}; int len=14; quickSort(num,len); for(int i=0;i<len;++i){ cout<<num[i]<<endl; } return 0; } void quickSort(int *num,int len){ if(len<=0) return; int left,right,key; key=num[0]; left=0; right=len-1; if(len>1){ while(left<right){ for(;right>left;right--){ if(num[right]<key){ num[left]=num[right]; break; } } for(;left<right;left++){ if(num[left]>key){ num[right]=num[left]; break; } } } num[left]=key; quickSort(num, left); quickSort(num+left+1,len-left-1); } }