啥是快排这里就不讲了,主要还是把原来c++学的东西抓紧转化过来 快排运用的是分而治之的思想,确定一个中值,把大的放右边,小的放左边,然后再左右分别对左右的左右(雾)进行处理 需要注意的一点是,这玩意远没有sort函数跑的快 function quickSort(arr){ if(arr.length <= 1){ return arr; } let l = [], r = []; let mid = Math.round(arr.length / 2); for(let i = 0; i <…
<script> var a = [7,4,5,3,2,1,4,5,6,6,2,21,4,53,12,0,-5,31,535,64,11,1,1,1,1]; function swap(arr, a, b){ var temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } function quickSort(arr,l,r){ if(l<r){ var left = l; var right = r; var temp = a[l]; whil…
今天工作室断网!果断回宿舍,不然各种资料都没有.(他说将来会找到!)不好意思,又哼起来了.进入主题,大家都知道,快排是各种排序算法中,最高效的也是应用最广的,还有更重要的一点,面试特别爱考的! 其实大家或多或少都听说过快排,也就是先从取出一个基准值,然后再把其它的数与之相对比,小的放左边的集合里,大的放右边的集合里,再通过递归不断重复该步骤,实现最高效率的quickSort. Talk is cheap, show you my code!…
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, whit…
二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number) { int start = 0; int end = length - 1; int index = 0; while(start < end) { index = start + (end - start)/2 if(a[index] == number){ return index; } else if(a[index] < number){…
// 进行一轮快排并返回当前的中间数 int getMiddle( int* arr, int low, int high ) { auto swaparr = [&]( int i, int j ) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }; ) { int k = arr[low], i = low, j = high; while( i != j ) { //R->L for( ; j > i &&…