当前位置:网站首页>Puge -- three basic sorting, bubbling, selection and quickness
Puge -- three basic sorting, bubbling, selection and quickness
2022-06-28 06:32:00 【Dear-JC】
List of articles
Bubble sort
- Compare adjacent elements . If the first one is bigger than the second one , Just swap them .
- Do the same for each pair of adjacent elements , From the beginning of the first couple to the end of the last couple . At this point , The last element should be the largest number .
- Repeat the above steps for all elements , Except for the last one .
- Keep repeating the above steps for fewer and fewer elements each time , Until there's no pair of numbers to compare .
public static void main(String[] args) {
int arr[] = {
18, 73, 54, 36, 64, 77};
for (int i = arr.length-1; i > 0; i--) {
//6 A digital , Yes 6 That's ok , From top to last line ,5 4 3 2 1 .
for (int j = 0; j < i; j++) {
// i The number of determines how many numbers are involved in bubbling // The numbers on each line participate in bubbling ;
int temp;
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
Selection sort
One 、 The basic idea
- The basic idea is this : First time from arr[0]~arr[n-1] Select the minimum value in , And arr[0] In exchange for ,
- Second times from arr[1]~arr[n-1] Select the minimum value in , And arr[1] In exchange for ,
- The third time from arr[2]~arr[n-1] Select the minimum value in , And arr[2] In exchange for ,…,
- The first i Secondary slave arr[i-1]~arr[n-1] Select the minimum value in , And arr[i-1] In exchange for ,…,
- The first n-1 Secondary slave arr[n-2]~arr[n-1] Select the minimum value in , And arr[n-2] In exchange for , Total pass n-1 Time , To get an ordered sequence from small to large .
Two 、 Sorting process
The original array :{25,103, 99, 46, 1}
The first 1 Secondary ranking
[1, 103, 99, 46, 25]----25 And 1 Interchange location
The first 2 Secondary ranking
[1, 25, 99, 46, 103]----25 And 103 Interchange location
The first 3 Secondary ranking
[1, 25, 46, 99, 103]----99 And 46 Interchange location
The first 4 Secondary ranking
[1, 25, 46, 99, 103]---- Is already an ordered array
————————————————
public static void main(String[] args) {
int arr[] = {
25, 103, 99, 46, 1};
for (int i = 0; i < arr.length-1; i++) {
int miniIndex = i; // Define an index pointer
int mini = arr[i];
for (int j = i+1; j < arr.length; j++) {
// take arr[0] Compare with the following in turn j Is the second number .
if (mini > arr[j]) {
// Compare , Put the small number forward , No small jump
// For index and arr[0] Redefinition . Take the minimum value every time .
miniIndex = j ;
mini = arr[j ]; // The smaller one gave mini
}
}
if (miniIndex != i) {
// When the indexes are different
arr[miniIndex] = arr[i]; // Replace the smaller value with .
arr[i] = mini; // Give the small value to the original position
}
System.out.println(" The first "+(i+1)+" Secondary ranking ");
System.out.println(Arrays.toString(arr));
}
}
Quick sort
- First set a boundary value , The array is divided into two parts by the boundary value
- Set data greater than or equal to the cut-off value to the right of the array , Data less than the cut-off value is set to the left of the array . here , Each element in the left part is less than the boundary value , The elements on the right are greater than or equal to the cut-off value ( It depends on how it is divided )
- then , The data on the left and right can be sorted independently . For the array data on the left , Another cut-off value can be taken , Divide the data into two parts , Also place smaller values on the left , Place the larger value on the right . The array data on the right can also be processed in a similar way
- Repeat the process , It can be seen that , This is a recursive definition . The left part is sorted recursively , Then recursively arrange the order of the right part . When left 、 After sorting the data in the two parts on the right , The sorting of the entire array is completed
public static void main(String[] args) {
int array[] = {
2, 4, 1, 6, 3, 5};
quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array)); // Convenient output array , Turn this array into String Type output .
}
public static void quickSort(int array[], int left, int right) {
if (left >= right) {
return;
}
int i = left; // 0
int j = right; // 5
int key = array[left]; // 2
while (i < j) {
// 0 5
while (i < j && array[j] > key) {
// 5 2 Compare the last number with the first number
j--; // If it's true , Move the right pointer forward
}
array[i] = array[j]; // 【1,4,1,6,3,5】 1 Assignment replaces the previous 2;
// Find the first ratio from back to front key Small numbers and array[i] In exchange for ;
while (i < j && array[i] < key) {
// 0 2
i++;
}
array[j] = array[i];
// From the past to the future, find the first one key Big numbers and array[j] In exchange for ;
}
array[i] = key;
// After a quick platoon, we have key Where to find .
quickSort(array, left, i - 1);
// Yes key Sort on the left
quickSort(array, i + 1, right);
// Yes key Sort the on the right
}
Welcome to Yazheng message !
边栏推荐
- 使用SSM框架,配置多个数据库连接
- CAD secondary development +nettopologysuite+pgis reference multi version DLL
- JDBC学习(一)——实现简单的CRUD操作
- Camx架构开UMD、KMD log以及dump图的方式
- sql及list去重操作
- FPGA - 7系列 FPGA SelectIO -09- 高级逻辑资源之IO_FIFO
- Boost the rising point | yolov5 combined with alpha IOU
- fpm工具安装
- Yolov5 adds a small target detection layer
- Differences between basic types and packaging classes
猜你喜欢

助力涨点 | YOLOv5结合Alpha-IoU

Triode driven brushless motor

Linux Mysql 实现root用户不用密码登录

How the third-party libraries in cocoapod reference local header files

微信小程序编译页面空白bug的原因

Iframe switching in Web Automation

Interpretation of Blog

freeswitch使用mod_shout模块播放mp3

Integer promotion and size side byte order

Parsing ng template with let total in NZ Pagination
随机推荐
Causes of wechat applet compilation page blank bug
AutoCAD C polyline self intersection detection
How the third-party libraries in cocoapod reference local header files
Caused by: com. fasterxml. jackson. databind. Exc.invalidformatexception: exception resolution
微信小程序编译页面空白bug的原因
mysql常用函数
选拔赛题目代码
Introduction to browser tools: think sky browser, team work browser
异常处理(一)——空指针和数组索引越界
Select trigger event from easyUI drop-down box
freeswitch设置最大呼叫时长
Yolact++ Pytorch环境
Eyebeam advanced settings
Caused by: com. fasterxml. jackson. databind. exc.InvalidDefinitionException: Cannot construct instance
图片按日期批量导入WPS表格
Alert pop-up processing in Web Automation
@The reason why the Autowired annotation is empty
Floating and positioning
Freeswitch sets the maximum call duration
Integer promotion and size side byte order