数据结构--冒泡排序

20151230 交流会

public class Sort {
        public Sort(){
        }

        //print array
        public void printArray(int a[], int cnt) {
                System.out.print(cnt + ":");
                for(int i=0; i<a.length; i++){
                        System.out.print(a[i] + " ");
                }
                System.out.println();
        }

        //swap value
        public void swap(int a[], int key1, int key2) {
                int t = a[key1];
                a[key1] = a[key2];
                a[key2] = t;
        }

        //Bubble Sort1
        public void bubbleSort1(int a[], int size) {
                System.out.println("Bubble sort1~");
                for(int i=0; i<size; i++) {
                        for(int j=1; j<size; j++) {
                                if(a[j-1] > a[j]) {
                                        swap(a, j-1, j);
                                        System.out.print("SWAP:");
                                        printArray(a, i);
                                }
                                System.out.print("     ");   
                                printArray(a, i);
                        }
                        //printArray(a);
                }
        }

        //Bubble Sort2
        public void bubbleSort2(int a[], int size) {
                System.out.println("Bubble sort2~");
                for(int i=0; i<size; i++) {
                        for(int j=1; j<size-i; j++) {
                                if(a[j-1] > a[j]) {
                                        swap(a, j-1, j);
                                        System.out.print("SWAP:");
                                        printArray(a, i);
                                }
                                System.out.print("     ");
                                printArray(a, i);
                        }
                        //printArray(a);
                }
        }

        //Bubble Sort3
        public void bubbleSort3(int a[], int size) {
                System.out.println("Bubble sort3~");
                boolean flag = true;
                for(int i=0; i<size && flag; i++) {
                        flag = false;
                        for(int j=1; j<size-i; j++) {
                                if(a[j-1] > a[j]) {
                                        swap(a, j-1, j);
                                        flag = true;
                                        System.out.print("SWAP:");
                                        printArray(a, i);
                                }
                                System.out.print("     ");
                                printArray(a, i);
                        }
                        //printArray(a);
                }
        }

        //main
        public static void main(String[] args) {
                Sort s = new Sort();
                int arr1[] = {49, 38, 65, 97, 76, 13, 27, 49};
                int arr2[] = {49, 38, 65, 97, 76, 13, 27, 49};
                int arr3[] = {49, 38, 65, 97, 76, 13, 27, 49};
                int size1 = arr1.length;
                int size2 = arr2.length;
                int size3 = arr3.length;

                s.printArray(arr1, 0);

                s.bubbleSort1(arr1, size1);
                s.bubbleSort2(arr2, size2);
                s.bubbleSort3(arr3, size3);

                int arr4[] = {27, 13, 38, 49, 49, 65, 76, 97};
                int arr5[] = {27, 13, 38, 49, 49, 65, 76, 97};
                int size4 = arr4.length;
                int size5 = arr5.length;

                s.bubbleSort2(arr4, size4);
                s.bubbleSort3(arr5, size5);

        }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值