当前位置:网站首页>CPT 102_ LEC 18
CPT 102_ LEC 18
2022-06-11 02:55:00 【NONE_ WHY】
1. Merge Sort
2. QuickSort
2.1. Info
2.1.1. Basic
- Invented by C.A.R. Hoare
- Used more widely than others
- works well for different types of data
- divide & conquer on sorting
2.1.2. Time Complexity
- Average O(N log N)
- Worst O(N^2)
2.2. Idea
- partition the array into two parts
- partitioning involves the selection of a[i] where the following conditions are met
- a[i] is in its final place in the array for some i
- none in a[1], ... , a[i-1] is greater than a[i]
- none in a[i+1], ... , a[r] is less than a[i]
- apply quicksort recursively to each part independently
2.3. Java
class Quick{ int[]target; public Quick(int[] target) { this.target = quickSort(target,0,target.length-1); } public int[] quickSort(int[] arr,int left,int right){ if (left < right) { int partitionIndex = partition(arr, left, right); quickSort(arr, left, partitionIndex - 1); quickSort(arr, partitionIndex + 1, right); } return arr; } // Partition private int partition(int[] arr, int left, int right) { // Set reference value (pivot) int pivot = left; int index = pivot + 1; for (int i = index; i <= right; i++) { if (arr[i] < arr[pivot]) { swap(arr, i, index); index++; } } swap(arr, pivot, index - 1); return index - 1; } private void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } @Override public String toString() { System.out.println("Quick Sort"); System.out.println("-------------------------"); System.out.println("After Sorting\t\t" + Arrays.toString(target)); return ""; } }
边栏推荐
- Jetpack compose scaffold and bottomappbar (bottom navigation)
- error exepected identifier before ‘(‘ token, grpc 枚举类编译错误
- MySQL is required to sort in ascending order greater than or equal to the current time, and then in descending order less than the current time
- HUST Software Engineering (Experiment 2) -- TDD test driven development experiment.
- Explanation of spark common parameters
- Three special data types, day3 and redis (geographic location, cardinality statistics and bitmap scene usage)
- Baidu submits sitemap to prompt the solution of "index type is not handled"
- 【冒泡排序的实现】
- How to add two factor authentication for WordPress websites
- WordPress upgrade error: briefly unavailable for scheduled maintenance [resolved]
猜你喜欢

蓝桥杯_小蓝吃糖果_鸽巢原理 / 抽屉原理

新来的同事问我 where 1=1 是什么意思???

CPT 102_ LEC 17

CPT 102_LEC 15

Istio安装与使用

2022年熔化焊接与热切割操作证考试题库及答案

Limiting visibility of symbols when linking shared libraries

Why did those who left Beijing, Shanghai and Guangzhou with a smile cry in the end?

同一个用户的两次请求SessionId竟然不一致-----记录问题

How can Delma's own brand "take off" when Philips is listed on the market?
随机推荐
Baidu submits sitemap to prompt the solution of "index type is not handled"
微信小程序
Manon's advanced road - Daily anecdotes
CPT 102_ LEC 20
判断一串数字是否是快速排序某一次的结果
P4338 [zjoi2018] history (tree section) (violence)
How to add cookie pop-up window in WordPress website (without plug-in)
[MySQL 45 -10] Lesson 10 how MySQL selects indexes
CPT 102_ LEC 17
Wechat applet
扁平数据转tree与tree数据扁平化
GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》
Error excluded identifier before '(' token, grpc enumeration class compilation error
GCC C inline assembly
同一个用户的两次请求SessionId竟然不一致-----记录问题
WordPress article directory plug-in luckywp table of contents setup tutorial
app 测试 常用 adb 命令集合
Sd3.0 notes
位置数据融合表3
How to add two factor authentication for WordPress websites