您的位置:首页 > 理论基础 > 数据结构算法

【原创】Java与数据结构(上篇:排序算法)

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

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

好了,下面附上各种排序算法的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
}
}
}
}


其他的数据结构内容请看下篇。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: