快速排序

算法学习——快速排序

喜夏-厌秋 提交于 2020-01-15 14:36:24
快速排序(分治算法) const int n = 1e6 + 10; int a[N]; //a[]:待排序数组,L:排序数组的起始下标,R:排序数组的结束下标 void quick_sort(int a[],int L,int R){ if(l >= r){return;} //递归的终止条件 int x = a[l]; //选取排序的比较对象 int i = L - 1; //设置排序的左指针 int j = R + 1; //设置排序的右指针 while(i<j){ //只要左右指针没相遇就循环进行 do i++; while( a[i]<x ); //左指针向右寻找 do j--; while( a[j]>x ); //右指针向左寻找 if(i<j){swap(a[i],a[j]); } //如果找到了就交换两个位置的变量 } quick_sort(a,L,j); //递归左区间 quick_sort(a,j+1,R) //递归右区间 } int main(){ int n; int a[10010]; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } quick(a,0,n-1); for(int i=0;i<n;i++){ cout<<a[i]<<” ”; } cout<<endl; } 来源: https://www.cnblogs

快速排序

空扰寡人 提交于 2020-01-15 10:25:11
public static void main(String[] args) { int[] array={49,38,65,97,76,13,27,50,78,34,12,64,1}; myQuickSort(array,0,array.length-1); System.out.println("排序之后:"); for(int i=0;i<array.length;i++){ System.out.print(array[i]+" "); } System.out.println(); } /** * 快排,是递归的,一个数组先进行一次轴划分成两个区, * 然后以轴为中心把左边的和右边都认为是一个新的数组再次进行快排 * @param array * @param low * @param hight */ public static void myQuickSort(int[] array,int low,int hight){ if(low<hight){ int axis = myFirstQuickSort(array, low, hight);//进行第一轮排序 myQuickSort(array,axis+1,hight);//右边认为是一个新的数组,进行快排 myQuickSort(array,low,axis-1);//左边的认为是一个新的数组,进行快排 }

快速排序与冒泡排序的效率对比

▼魔方 西西 提交于 2020-01-15 08:54:08
快速排序与冒泡排序的效率对比 鄙人在这里谈一下自己对冒泡排序与快速排序的看法,在经过详细的浏览代码以及文章后,我们可以得出以下结论: 1 快速排序是冒泡排序的一种改进,跟冒泡排序有一定的区别。 2. 首先快速排序是选取一个基准数值(数值可以是任何数字),然后在与其他的数字进行比较,比基准数大的放在基准数的左边女,比基准数小的放在右边,与基准数相等的可以任意放置。 3. 把基准数分成两个数列,然后在将两个数列进行比较。 来源: CSDN 作者: ghijrf 链接: https://blog.csdn.net/ghijrf/article/details/103787006

NEFU 快速排序和桶排序

倖福魔咒の 提交于 2020-01-15 04:07:45
快速排序 sort函数:对数组排序 sort(x,x+n) / 默认升序 / 升序:sort(x,x+n,less<数据类型>()) 降序:sort(x,x+n,greater<数据类型>()) 老和尚的导员 # include <bits/stdc++.h> using namespace std ; struct sa { int c , d , g ; int y , h , b ; } x [ 110 ] ; //cmp函数的写法 int cmp ( const sa & a , const sa & b ) //const是常量的意思不加可能会WA { if ( a . h != b . h ) return a . h > b . h ; //返回降序,改成<可返回升序 else if ( a . c != b . c ) return a . c > b . c ; else if ( a . d != b . d ) return a . d > b . d ; else if ( a . g != b . g ) return a . g > b . g ; else if ( a . y != b . y ) return a . y > b . y ; } int main ( ) { int n ; while ( scanf ( "%d" , & n )

双边循环法下的快速排序

北城余情 提交于 2020-01-15 03:52:11
(1)选定基准元素pivot,并且设置两个指针left和right,指向数列的最左和最右边两个元素。 (2)接下来进行第一次循环,从right指针开始,让指针所指向元素和基准元素做比较。如果大于或者等于pivot,则指针向左移动;如果小于pivot,则right指针停止移动,切换到left指针。 (3)left指针移动时,让指针所指向的元素和基准元素作比较。如果小于或等于pivot,则指针向右移动;如果大于pivot,则left指针停止移动。 (4)直到left指针和right指针重合,将基准元素pivot与指针重合处所指向内容交换。第一轮交换结束。具体代码如下所示 package com.company; import java.lang.reflect.Array; import java.util.Arrays; public class quickSort { public static void quickSort(int[] arr,int startIndex,int endIndex) { // 递归结束条件:startIndex大于或者等于endIndex时 if(startIndex>=endIndex){ return; } //得到基准元素位置 int pivotIndex=partition(arr,startIndex,endIndex); /

Java 快速排序

时光毁灭记忆、已成空白 提交于 2020-01-15 01:37:21
/** * @author wkk * @date 2020/1/9 11:09 */ public class MyTestQuickSort { public static void main ( String [ ] args ) { int [ ] arr = { 213 , 499 , 1 , 53 , 56 , 3 , 4 , 89 , 0 , 388 , 45 , 34 } ; quickSort ( arr , 0 , arr . length - 1 ) ; System . out . println ( Arrays . toString ( arr ) ) ; } private static void quickSort ( int [ ] arr , int min , int max ) { if ( min == max || min > max ) { return ; } int index = partition ( arr , min , max ) ; quickSort ( arr , min , index ) ; quickSort ( arr , index + 1 , max ) ; } /** * 分割数组 * <p> * 核心内容 */ private static int partition ( int [ ] arr ,

基本算法——快速排序(左右指针法)

人走茶凉 提交于 2020-01-14 16:47:27
注:使用左右指针时,如果选择数组左边为标准点,则需要先移动右边指针 一、算法核心思想 不断将一个数组中 大 的数放在选取的标准数 右边 ,比标准数 小 的放在标准数 左边 (一般情况,当然你可以小的放左边大的放右边) 二、图解 三、代码 /** * 快速排序 前后指针 * 侧重注意,如果是以前面为标准key,需要先从最后开始移动指针 */ public static int[] quickSort2(int[] array, int low, int high){ if(low < high){ //此时设第一个数为key int key = array[low], i = low, j = high; while (i < j){ //从右边开始移动指针 //此时右边的值都大于key,不需要交换,继续指针向前移动 while (i < j && array[j] >= key){ j--; } while (i < j && array[i] <= key){ i++; } if(i < j){ int temp = array[i]; array[i] = array[j]; array[j] = temp; } } //交换key和重合指针的值 array[low] = array[i]; array[i] = key; //递归 quickSort2(array, low,

js快速排序

旧时模样 提交于 2020-01-14 10:51:16
/** * 快速排序 arr:[]排序的数组 type:true 小到大 false大到小 filed:如过比的是对象里面的谋个值,则传字段 */ export function listSort(obj: any): Array { const { arr, type = true, filed = false } = obj; const len = arr.length; if (len <= 1) { return arr; } const pivotInedex = Math.floor(len / 2); const pivot = arr.splice(pivotInedex, 1)[0]; let left = []; let right = []; for (let i = 0; i < arr.length; i++) { const obj = arr[i]; let bool = true; if (type) { bool = filed ? obj[filed] < pivot[filed] : obj < pivot; } else { bool = filed ? obj[filed] > pivot[filed] : obj > pivot; } if (bool) { left.push(obj); } else { right.push(obj

快速排序

℡╲_俬逩灬. 提交于 2020-01-14 04:50:02
快速排序的主要思想是partition思想,其大致内容如下: 1.随机选择基准值,一般是第一个数; 2.利用头尾指针,头指针负责找第一个比基准值大的数,尾指针负责找第一个比基准值小的数,交换两个指针的内容; 3.将基准值放到其应该属于的位置; 4.递归地进行以基准值分成的左右部分; c++代码 //快速排序 void quick_sort ( int arr [ ] , int start , int end ) { if ( start < end ) { //partition过程 //基准值 int key = arr [ start ] ; //前后扫描用的指针 int left = start ; int right = end ; while ( left < right ) { //right从右向左负责找第一个比基准值小的值 while ( left < right && arr [ right ] >= key ) { -- right ; } if ( left < right ) arr [ left ++ ] = arr [ right ] ; //left从左向右负责找第一个比基准值大的值 while ( left < right && arr [ left ] < key ) { ++ left ; } if ( left < right ) arr [

动画 | 什么是快速排序?

别来无恙 提交于 2020-01-13 04:01:42
快速排序属性 上一篇文章介绍了 冒泡排序和它的优化 。这次介绍的快速排序是冒泡排序演变而来的算法,比冒泡排序要高效的很多。 快速排序之所以快,是因为它使用了分治法。它虽然也是通过不断的比较和移动来实现排序的,只不过它的实现,增大了比较的距离和移动的距离。而冒泡排序只是相邻的比较和交换。 快速排序的思想是,通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序的目的。 从字面上感觉不到它的好处,我们通过一个示例来理解基本的快速排序算法,假设当前数组元素是:5, 1, 9, 3, 7, 4, 8, 6, 2。 基本的快速排序算法 初始状态:5, 1, 9, 3, 7, 4, 8, 6, 2 选择5作为一个基准元素,然后从右向左移动hight下标,进行基准元素和下标为hight 的元素进行比较。 如果基准元素要大,则进行hight的左移一位;如果基准元素要小,则进行元素的交换。 在hight下标左移的过程中,我们目的是找出比基准元素小的,然后进行交换。 交换完之后,进行left的右移,找出比基准元素大的,找到则进行交换。 视频动画 Code Result 发生交换 [2, 1, 9, 3, 7, 4, 8, 6, 5] 发生交换 [2, 1, 5, 3, 7, 4, 8, 6, 9] 发生交换