Java与数据结构(上篇:排序算法)

14 篇文章 0 订阅
14 篇文章 0 订阅

 花了两天的时间坐在图书馆里,终于写完了所有主要的数据结构,包括其中的算法部分,呵呵,保研和面试的第一关估计没问题了,下面就是看OS和Network了

 

 

http://www.cnblogs.com/yinger/archive/2012/08/21/2649626.html

心得:纸上得来终觉浅,绝知此事要躬行!当自己回想着算法的整个过程,然后一行一行的敲下来,发现算法太精辟了,看似简单,写起来可真不是那么回事,而且,写多了,熟悉了,思路就快了,写起来就得心应手了!可能你会觉得算法和数据结构对现在的程序员来说已经不重要了,但是,我觉得,好的程序员如果不懂数据结构和算法那么他就写不出更好的代码!作为一个计科男,掌握数据结构和算法是必须的!

 

好了,下面附上各种排序算法的Java实现,算法的思想不介绍了,网上有很多,如果发现我的代码中有什么错误,也请及时回复我哦!谢谢

 

1. 冒泡排序

复制代码
package SortAlgorithm;

import java.util.Arrays;

/**
 * 冒泡排序
 * 
 * @author yinger
 */

public class BubbleSort {

    public static void main(String[] args) {
        int[] s = new int[] { 10, 23, 43, 9, 25, 87, 56, 34, 11 };
        System.out.println("Before: " + Arrays.toString(s));// Before: [10, 23, 43, 9, 25, 87, 56, 34, 11]
        sort(s);// Arrays.sort(s);
        System.out.println("After: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
    }

    // bubble sort --- time: O(n^2)
    public static void sort(int[] s) {
        int n = s.length;
        int i, j, temp, flag;// flag: change happened?
        for (i = n - 1; i >= 1; i--) {// number from end to begin
            flag = 0;
            for (j = 0; j < i; j++) {// range is smaller
                if (s[j] > s[j + 1]) {
                    temp = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = temp;
                    flag = 1;
                }
            }
            if (flag == 0) {// nothing change, sort is done
                break;
            }
        }
    }
}
复制代码

 

2. 选择排序

复制代码
package SortAlgorithm;

import java.util.Arrays;

/**
 * 选择排序
 * 
 * @author yinger
 */

public class SelectSort {

    public static void main(String[] args) {
        int[] s = new int[] { 10, 23, 43, 9, 25, 87, 56, 34, 11 };
        System.out.println("Before: " + Arrays.toString(s));// Before: [10, 23, 43, 9, 25, 87, 56, 34, 11]
        sort(s);// Arrays.sort(s);
        System.out.println("After: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
    }

    // select sort --- time: O(n^2)
    public static void sort(int[] s) {
        int n = s.length;
        int i, j, min, minIndex, temp;// minIndex: the index of the min number
        for (i = 0; i < n - 1; i++) {// every time select the min number
            minIndex = i;
            min = s[minIndex];
            for (j = i + 1; j <= n - 1; j++) {
                if (s[j] < min) {
                    minIndex = j;
                    min = s[j];
                }
            }
            if (minIndex != i) {// only one change
                temp = s[i];
                s[i] = s[minIndex];
                s[minIndex] = temp;
            }
        }
    }
}
复制代码

 

3. 直接插入排序

复制代码
package SortAlgorithm;

import java.util.Arrays;

/**
 * 直接插入排序
 * 
 * @author yinger
 */

public class DirectInsertSort {

    public static void main(String[] args) {
        int[] s = new int[] { 10, 23, 43, 9, 25, 87, 56, 34, 11 };
        System.out.println("Before: " + Arrays.toString(s));// Before: [10, 23, 43, 9, 25, 87, 56, 34, 11]
        sort(s);// Arrays.sort(s);
        System.out.println("After: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
    }

    // direct insert sort --- time: O(n^2)
    public static void sort(int[] s) {
        int n = s.length;
        int i, j, temp;
        for (i = 1; i < n; i++) {// begin with 1,end with (n-1)
            temp = s[i];
            for (j = i - 1; j >= 0 && s[j] > temp; j--) {// condition: current one (s[i]) is smaller
                s[j + 1] = s[j];// move back one step
            }
            s[j + 1] = temp;// final position:j+1
        }

    }
}
复制代码

 

4. 折半插入排序

复制代码
package SortAlgorithm;

import java.util.Arrays;

/**
 * 折半插入排序
 * 
 * @author yinger
 */

public class HalfFindInsertSort {

    public static void main(String[] args) {
        int[] s = new int[] { 10, 23, 43, 9, 25, 87, 56, 34, 11 };
        System.out.println("Before: " + Arrays.toString(s));// Before: [10, 23, 43, 9, 25, 87, 56, 34, 11]
        sort(s);// Arrays.sort(s);
        System.out.println("After: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
    }

    // half find insert sort --- time: O(n^2)
    public static void sort(int[] s) {
        int n = s.length;
        int i, j, temp;
        int low, high, mid;
        for (i = 1; i < n; i++) {// begin with 1,end with (n-1)
            temp = s[i];
            low = 0;
            high = i - 1;
            while (low <= high) {// find the insert position --- low
                mid = (low + high) / 2;
                if (s[mid] > temp) {
                    high = mid - 1;
                } else {// equal in here! equal can be ignored
                    low = mid + 1;
                }
            }
            for (j = i; j > low; j--) {// move back one step
                s[j] = s[j - 1];
            }
            s[low] = temp;
        }

    }
}
复制代码

 

5. 快速排序

复制代码
package SortAlgorithm;

import java.util.Arrays;

/**
 * 快速排序
 * 
 * @author yinger
 */

public class QuickSort {

    public static void main(String[] args) {
        int[] s = new int[] { 10, 23, 43, 9, 25, 87, 56, 34, 11 };
        System.out.println("Before: " + Arrays.toString(s));// Before: [10, 23, 43, 9, 25, 87, 56, 34, 11]
        sort(s);// Arrays.sort(s);
        System.out.println("After: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
    }

    // quick sort --- time: O(n*log(n))
    public static void sort(int[] s) {
        int n = s.length;
        subSorrt(s, 0, n - 1);
    }

    private static void subSorrt(int[] s, int start, int end) {
        // System.out.println(" start=" + start + "  end=" + end);
        if (start < end) {// need to sort
            int low, high, standard;
            low = start;
            high = end;
            standard = s[start];// the standard number
            while (low < high) {
                while (low < high && s[high] > standard) {// move high pointer
                    high--;
                }// end while --> s[high]<standard
                s[low] = s[high];// s[low] has been saved! --> after this,s[high] has changed to s[low] and saved!
                low++;
                while (low < high && s[low] < standard) {// move low pointer
                    low++;
                }// end while --> s[low]>standard
                s[high] = s[low];
                high--;
            }// end while
            if (low > high) {
                low--;//attention: if low = high,then position is low,but if low > high,then position is high = low-1
            }
            s[low] = standard;// final,end the while loop,standard number should be in (low-1) index
            // System.out.println(" standard=" + standard + "  position=" + low);
            // System.out.println("Current Array: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
            subSorrt(s, start, low - 1);// sort left
            subSorrt(s, low + 1, end);// sort right
        }// else do nothing
    }
}
复制代码

 

6. 堆排序

复制代码
package SortAlgorithm;

import java.util.Arrays;

/**
 * 堆排序
 * 
 * @author yinger
 */

public class HeapSort {

    public static void main(String[] args) {
        int[] s = new int[] { 10, 23, 43, 9, 25, 87, 56, 34, 11 };
        System.out.println("Before: " + Arrays.toString(s));// Before: [10, 23, 43, 9, 25, 87, 56, 34, 11]
        sort(s);// Arrays.sort(s);
        System.out.println("After: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
    }

    // heap sort --- time: O(n*log(n))
    public static void sort(int[] s) {
        int n = s.length;
        int last = (n - 2) / 2;// the last node that is not leaf node = the parent of last node is (n-1-1)/2
        int i, j, temp;// minIndex = the index of the min node
        for (i = last; i >= 0; i--) {// first:build a min heap
            adjustHeap(s, i, n - 1);// then adjust the heap
        }
        System.out.println("Build Heap: " + Arrays.toString(s));// Build Heap: [9, 10, 43, 11, 25, 87, 56, 34, 23]
        for (j = n - 1; j > 0; j--) {
            temp = s[0];// first:change the start node and the end node
            s[0] = s[j];
            s[j] = temp;
            adjustHeap(s, 0, j - 1);// after change,adjust the heap
            System.out.println("Current Heap: " + " j = " + j + "   " + Arrays.toString(s));
        }
    }

    private static void adjustHeap(int[] s, int start, int end) {// adjust the heap from start node to end node
        int j, l, r, min, minIndex, temp;// minIndex = the index of the min node
        j = start;
        temp = s[start];// save it!
        while (2 * j + 1 <= end) {//2 * j + 1 <= end means in giving range,node j still has child
            l = 2 * j + 1;// left node
            r = 2 * j + 2;// right node --- not always exist
            if (r <= end && s[r] < s[l]) {// find the min node of childe nodes
                min = s[r];// right node is smaller
                minIndex = r;
            } else {
                min = s[l];// left node is smaller
                minIndex = l;
            }
            if (temp > min) {// parent is bigger,so change!
                s[j] = min;
                s[minIndex] = temp;
                j = minIndex;// change the index of the node,it means the current node has moved to index j
            } else {
                break;// if not,break the while loop
            }
        }
    }
}
复制代码

 

 

7. 二路归并排序

复制代码
package SortAlgorithm;

import java.util.Arrays;

/**
 * 合并排序
 * 
 * @author yinger
 */

public class MergeSort {

    public static void main(String[] args) {
        int[] s = new int[] { 10, 23, 43, 9, 25, 87, 56, 34, 11 };
        System.out.println("Before: " + Arrays.toString(s));// Before: [10, 23, 43, 9, 25, 87, 56, 34, 11]
        sort(s);// Arrays.sort(s);
        System.out.println("After: " + Arrays.toString(s));// After: [9, 10, 11, 23, 25, 34, 43, 56, 87]
    }

    // merge sort --- time: O(n*log(n))
    public static void sort(int[] s) {
        int n = s.length;
        mergesort(s, 0, n - 1);
    }

    private static void mergesort(int[] s, int start, int end) {//merge from start to end
        if (start < end) {
            int mid = (start + end) / 2;
            mergesort(s, start, mid);// sort left
            mergesort(s, mid + 1, end);// sort right
            int i = start, j = mid + 1, k, len;// left and right are all sorted,then merge them!
            len = end - start + 1;
            int[] temp = new int[len];//use another array to merge
            for (k = 0; k < len; k++) {// i range (start,mid),and j range (mid+1,end)
                if ((i <= mid && j <= end && s[i] < s[j]) || j > end) {// if j>end,then copy must copy i
                    temp[k] = s[i];
                    i++;
                } else {
                    temp[k] = s[j];
                    j++;
                }
            }
            for (k = 0; k < len; k++) {// temp is sorted array
                s[k + start] = temp[k];// copy temp to s
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值