想總結(jié)一下這個問題是因為這是找工作的時候必考的東西。我最近很擔(dān)心會被解雇。以后再什么事也不上心可不行。到了一個新環(huán)境畢竟不像在大學(xué)里那么游刃有余了。
接收數(shù)組的java源程序:
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Sort {
int Length = 0;
public int[]Input(){
try {
BufferedReader num = new BufferedReader(new InputStreamReader(
System.in));
Length = Integer.parseInt(num.readLine());
} catch (Exception e) {
e.printStackTrace();
}//從客戶端接收要排序的數(shù)組的長度。
int[] a = new int[Length];
try {
for (int i = 0; i < a.length; i++) {
BufferedReader array_int = new BufferedReader(new InputStreamReader(
System.in));
a[i] = Integer.parseInt(array_int.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}//從客戶端接收要排序的數(shù)組。
return(a);
}
}
上面一段代碼主要完成接收要排序的數(shù)組的功能。也可以自己指定一數(shù)組,不影響這里要講的主要問題。
直接插入排序:
算法思想:每次將一個待排序的數(shù)據(jù)元素,插入到前面已經(jīng)排好序的數(shù)列中的適當(dāng)位置,使數(shù)列依然有序;直到待排序數(shù)據(jù)元素全部插入完為止。
java 源程序:
package sort.insertSort;
import java.util.Arrays;
public class DirectInsertSort {
private static Integer[] insert(Integer[] sourceNums) {
//如果沒有要排序的數(shù)據(jù),直接返回
if(sourceNums == null || sourceNums.length == 0) {
return new Integer[]{};
}
Integer nums[] = new Integer[sourceNums.length];//已排好序的數(shù)組。
nums[0] = sourceNums[0];//先將一個數(shù)值放到nums中(因為只有一個數(shù),也可以看作排好序的。
for(int i=1; i<sourceNums.length; i++) {
Integer num = sourceNums[i];
for(int j=i; j>0; j--) {
if(num >= nums[j-1]){//如果要插入的數(shù)值不小于最后一個數(shù)值,則直接插入到最后面。
nums[j] = num;
break;
} else if(num < nums[j-1]) {//如果要插入的數(shù)值小于nums最后一個數(shù)值,則將nums當(dāng)前數(shù)值后移一位
nums[j] = nums[j-1];
if(j == 1) {
nums[j-1] = num;//應(yīng)對當(dāng)要插入的數(shù)小于nums所有數(shù)的情況
}
}
}
}
return nums;
}
public static void main(String[] args) {
System.err.println(Arrays.toString(DirectInsertSort.insert(new Integer[]{34,56,78,-11,49,-45,-90,85})));
}
}
選擇排序:
算法思想:每一趟從待排序的數(shù)據(jù)元素中選出最小(或最大)的一個元素,順序放在已排好序的數(shù)列的最后,直到全部待排序的數(shù)據(jù)元素排完。
public class SelectSort extends Sort{
public static void main(String args[]) {
Sort sort=new Sort();
int[] a=sort.Input(); //調(diào)用父類函數(shù),接收字符串。
int q;
int i=0;
while(i<a.length){
int j=i;
while(j<a.length){
if(a[i]>a[j]){
q=a[i];
a[i]=a[j];
a[j]=q;
}
j++;
}
i++;
}//while循環(huán)部分完成排序。
for(int j=0;j<a.length;j++){
System.out.print(a[j]+" ");
}
}
}
冒泡排序 :
算法思想:兩兩比較待排序數(shù)據(jù)元素的大小,發(fā)現(xiàn)兩個數(shù)據(jù)元素的次序相反時即進行交換,直到?jīng)]有反序的數(shù)據(jù)元素為止。
package sort.bubbleSort;
import java.util.Arrays;
public class BubbleSort {
static Integer[] bubbleSort(Integer[] nums) {
boolean change = true;
int i;
for(i=0; i< nums.length-1 && change; i++) {
change = false;
for(int j=0; j<nums.length-i-1; j++) {
if(nums[j]>nums[j+1]) {
int num = nums[j];
nums[j] = nums[j+1];
nums[j+1] = num;
change = true;
}
}
System.err.println(Arrays.toString(nums));
}
return nums;
}
public static void main(String[] args) {
BubbleSort.bubbleSort(new Integer[]{34,56,78,-11,49,-45,-90,85,65,63,-12,33,35});
}
}
希爾排序(縮小增量法)
算法思想:先取一個正整數(shù)d1<n,把所有相隔d1的記錄放一組,組內(nèi)進行直接插入排序;然后取d2<d1,重復(fù)上述分組和排序操作;直至di=1,即所有記錄放進一個組中排序為止
public class ShellSorter extends Sort {
public static void main(String args[]) {
Sort sort = new Sort();
int[] a = sort.Input();
int q = 0;
int group = a.length / 3 + 1;
while(group > 0) {
int i = 0;
while (i < group) {
int j = i;
while ( j < a.length) {
int k = j;
while ( k > i) {
if (a[k] < a[k - group]) {
q = a[k];
a[k] = a[k -group];
a[k - group] = q;
}
k -= group;
}
j += group;
}
i++;
}
group--;
}
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
}
}
posted on 2007-04-18 10:05
靜兒 閱讀(425)
評論(7) 編輯 收藏 所屬分類:
技術(shù)