欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

冒泡排序 J# 

程序员文章站 2022-07-12 11:07:56
...

交换排序法-----冒泡排序 整体思路(已两两交换) 先找出数组中最大(小)的元素放在其位置
     */

   
static void BubbleSort(int[] sqList, boolean is_smallTbig) {
       
int sentinel = 0;
       
int i = sqList.length, j = 0;
       
if (is_smallTbig) {
           
for (boolean change = true; i > 0 && change; i--) {
                change
= false;// 如何已经有序则不用拍
                for (j = 1; j < i; j++) {
                    sentinel
= sqList[j];
                   
if (sentinel < sqList[j - 1]) {
                        change
= true;
                        sqList[j]
= sqList[j - 1];
                        sqList[j
- 1] = sentinel;
                    }

                }
            }
        }
else {
           
for (boolean change = true; i > 0 && change; i--) {
                change
= false;// 如何已经有序则不用拍
                for (j = 1; j < i; j++) {
                    sentinel
= sqList[j];
                   
if (sentinel > sqList[j - 1]) {
                        sqList[j]
= sqList[j - 1];
                        sqList[j
- 1] = sentinel;
                        change
= true;
                    }

                }
            }
        }

    }

相关标签: J#