quickSort in-place version whose space complexity is O(log(N))

 1 public void quickSortSwapping(int data[]){
 2 //call this method
 3 quickSortSwapping(data,0,data.length);
 4 }
 5
 6
 7 public void quickSortSwapping(int data[],int start,int len){
 8 if(len<2)return;
 9 int pivotIndex=start+len/2;
10 int pivotValue=data[pivotIndex];
11 int end=data.length;
12 int cur=start;
13
14 //swapping the pivot to the end of array,for the testcase [a,a,a,a,a,a](PIE p130)
15
16 swap(data,pivotIndex,--end);
17
18 //partiton the rest of array
19 while(cur<end){
20
21 if(data[cur]<pivotVaule){
22
23 cur++;
24
25 }else{
26
27 swap(data,cur,--end);
28
29 }
30
31 //put pivot back to the original place
32
33 swap(data,end,start+len-1);
34
35 //using recursive method to partition ench part
36
37 int llen=end-start;
38 int rlen=len-llen-1;
39
40 if(llen>1){
41
42 quickSortSwapping(data,start,llen);
43 }
44
45 if(rlen>1){
46
47 quickSortSwapping(data,end+1,rlen);
48 }
49 }
50
51
52 }
时间: 2024-08-24 23:31:08

quickSort in-place version whose space complexity is O(log(N))的相关文章

空间复杂度是什么?What does ‘Space Complexity’ mean? ------geeksforgeeks 翻译

这一章比较短! 空间复杂度(space complexity)和辅助空间(auxiliary space)经常混用,下面是正确的辅助空间和空间复杂度的定义 辅助空间:算法需要用到的额外或者暂时的存储空间. 空间复杂度:是指算法所需要的所有存储空间,这是跟输入数据的大小决定的.空间复杂度包括辅助空间和保存输入的存储空间. 如果我们想比较几个标准的排序算法所需要的空间,那么用辅助空间来分析会比空间复杂度好.归并排序用了O(n)的辅助空间.而插入排序和堆排序用的O(1)的辅助空间.但是他们这些算法的空

Sort a linked list in O(n log n) time using constant space complexity.

1 利用归并的方法进行排序 2 完成两个主要功能即可:拆分和合并 3 拆分用到了比较好的方法就是利用快慢指针进行,每次找到链表的中间部分进行拆解 4 合并可以利用两种方式:一种是非递归的方法另一种是递归的方法,都是可以做的 5 个人比较喜欢递归的方法,理解起来比较简单 6 /** 7 * Definition for singly-linked list. 8 * struct ListNode { 9 * int val; 10 * ListNode *next; 11 * ListNode(

Left Shift Array with O(l) time complexity and O(1) space complexity

Algorithm 1: public static void main(String[] args) { int[] a = new int[]{1,2,3,4,5,6,7,8,9,10,11,12}; shiftN2(a, 1); System.out.println(Arrays.toString(a)); } public static void shiftN2(int[] a, int n) { n = n%a.length; swapArray(a, 0, n-1); swapArr

Leetcode version 2

148. Sort List 题目:Sort a linked list in O(n log n) time using constant space complexity. 题意:排序链表 思路I:merge sort 复杂度分析:时间复杂度O(nlgn),空间复杂度O(1) 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 *

165. Compare Version Numbers比较版本号的大小

[抄题]: Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . cha

[Daily Coding Problem 223] O(1) space in order traversal of a binary tree

Typically, an implementation of in-order traversal of a binary tree has O(h) space complexity, where h is the height of the tree. Write a program to compute the in-order traversal of a binary tree using O(1) space. In-order traversal without recursio

Basic Sort Algorithms

1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < arr.length - j; i++) { if (arr[i] > arr[i + 1]) { tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1

Complexities

This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and

【Unity3D自学记录】可视化对照十多种排序算法(C#版)

在这篇文章中.我会向大家展示一些排序算法的可视化过程.我还写了一个工具.大家可对照查看某两种排序算法. 下载源代码 – 75.7 KB 下载演示样例 – 27.1 KB 引言 首先,我觉得是最重要的是要理解什么是"排序算法".依据维基百科.排序算法(Sorting algorithm)是一种能将一串数据按照特定排序方式进行排列的一种算法. 最经常使用到的排序方式是数值顺序以及字典顺序.有效的排序算法在一些算法(比如搜索算法与合并算法)中是重要的,如此这些算法才干得到正确解答.排序算法也