生活随笔
收集整理的這篇文章主要介紹了
冒泡排序的多种写法、逻辑
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
冒泡排序的多種寫法、邏輯 本文提供全流程,中文翻譯。
Chinar堅持將簡單的生活方式,帶給世人!
(擁有更好的閱讀體驗 —— 高分辨率用戶請根據需求調整網頁縮放比例) |
- 1
- 2
- Bubble Sort —— 冒泡排序( - 根據下標)
- 3
- Bubble Sort —— 冒泡排序( - 根據輪數)
- 4
- Bubble Sort —— do while ( - 判定是否滿足有序)
1
Bubble Sort —— 上推分類法
本方法重點在于根據次第來完成運算
1 —— 把最小數移動到最后一位
2 —— 由于最后一位確定,所以在第二次運算時不用計算。所以第二次計算次數 減 1
3 —— 依次類推,就可用簡單的for循環來達到排序的目的
4 —— 具體由大到小,還是由小到大。根據自己的需要調節 ” > < ” 的比較方式,交換順序不需要變化。
數組中:a[0]與a[1] 位置交換的方式,不使用中間變量的情況下 ↓
a[j] = a[j] + a[j + 1]; //a[0]=10+120 a[j + 1] = a[j] - a[j + 1]; //a[1]=130-120 —— (10)a[j] = a[j] - a[j + 1]; //a[0]=130-10 —— (120)
using System;namespace TestMaoPao
{class BubbleSort{
static void Main(
string[] args){
int[] list = {
10,
8,
3,
5,
6,
7,
9,
0,
1,
2 };
int count =
0;count = Sort(list);Console.WriteLine(count);Console.WriteLine(
"**********************");
for (
int i =
0; i < list.Length; i++){Console.WriteLine(list[i]);}Console.ReadKey();}
private static int Sort(
int[] a){
int count =
0;
for (
int i = a.Length -
1; i >
0; i--){
for (
int j =
0; j < i; j++){count++;
if (a[j] < a[j +
1]){a[j] = a[j] + a[j +
1]; a[j +
1] = a[j] - a[j +
1]; a[j] = a[j] - a[j +
1]; }}}
return count;}}
}
最后輸出結果為:
45 (運算次數)
**********************
10
9
8
7
6
5
3
2
1
0
2
Bubble Sort —— 冒泡排序( - 根據下標)
本方法重點在于根據下標來完成運算
1 —— 把最小數移動到第一位
2 —— 由于第一位最小值確定,所以在第二次運算時不用計算。所以第二次計算時,從下標 list[1](也就是第二個數)開始做對比
3 —— 依次類推,就可用簡單的for循環來達到排序的目的
4 —— 具體由大到小,還是由小到大。根據自己的需要調節 ” > < ” 的比較方式,交換順序不需要變化。
數組中:a[0]與a[1] 位置交換的方式,使用中間變量的情況下 ↓
int tempNumber = a[j]; ; //tempNumber = 8a[j] = a[i]; //a[1]=10 a[i] = tempNumber; //a[0]=8
using System;namespace TestMaoPao
{class BubbleSort{
static void Main(
string[] args){
int[] list = {
10,
8,
3,
5,
6,
7,
9,
0,
1,
2 };
int count =
0;count = Sort(list);Console.WriteLine(count);Console.WriteLine(
"**********************");
for (
int i =
0; i < list.Length; i++){Console.WriteLine(list[i]);}Console.ReadKey();}
private static int Sort(
int[] a){
int count =
0;
for (
int i =
0; i < a.Length; i++){
for (
int j = i +
1; j < a.Length; j++){count++;
if (a[j] < a[i]) {
int tempNumber = a[j]; ; a[j] = a[i]; a[i] = tempNumber; }}}
return count;}}
}
最后輸出結果為:
45 (運算次數)
**********************
0
1
2
3
5
6
7
8
9
10
3
Bubble Sort —— 冒泡排序( - 根據輪數)
本方法重點在于根據次第來完成運算
1 —— 把最小數移動到第一位
2 —— 由于第一位最小值確定,所以在第二次運算時不用計算。所以第二次計算時,從下標 list[1](也就是第二個數)開始做對比
3 —— 依次類推,就可用簡單的for循環來達到排序的目的
4 —— 具體由大到小,還是由小到大。根據自己的需要調節 ” > < ” 的比較方式,交換順序不需要變化。
最后輸出結果為:
45 (運算次數)
**********************
0
1
2
3
5
6
7
8
9
10
4
Bubble Sort —— do while ( - 判定是否滿足有序)
本方法重點在于:判定滿足依次排列,有序后跳出循環
數組中:a[0]與a[1] 位置交換的方式,不使用中間變量的情況下 ↓
a[j] = a[j] + a[j + 1]; //a[0]=10+120 a[j + 1] = a[j] - a[j + 1]; //a[1]=130-120 —— (10)a[j] = a[j] - a[j + 1]; //a[0]=130-10 —— (120)
using System;namespace TestMaoPao
{class BubbleSort{
static void Main(
string[] args){
int[] ints = {
5,
4,
3,
8,
9,
1,
7,
2,
0,
6,
10};Short(ints);
foreach (
var i
in ints){Console.WriteLine(i);}Console.ReadLine();}
static void Short(
int[] ints){
int count =
0;
bool isStop;
do{isStop =
false;
for (
int i =
0; i < ints.Length -
1; i++) {
if (ints[i] > ints[i +
1]){ints[i +
1] = ints[i] + ints[i +
1];ints[i] = ints[i +
1] - ints[i];ints[i +
1] = ints[i +
1] - ints[i];isStop =
true; count++;}}}
while (isStop);Console.WriteLine(
"循環次數: " + count);}}
}
最后輸出結果為:
循環次數: 27
0
1
2
3
4
5
6
7
8
9
10
END 本博客為非營利性個人原創,除部分有明確署名的作品外,所刊登的所有作品的著作權均為本人所擁有,本人保留所有法定權利。違者必究
對于需要復制、轉載、鏈接和傳播博客文章或內容的,請及時和本博主進行聯系,留言,Email: ichinar@icloud.com
對于經本博主明確授權和許可使用文章及內容的,使用時請注明文章或內容出處并注明網址
轉載于:https://www.cnblogs.com/chinarbolg/p/9601478.html
總結
以上是生活随笔為你收集整理的冒泡排序的多种写法、逻辑的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。