[英]Insertion Sort?
所以我們的老師給了我們這個代碼進行冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int maxSize = 100;
ArrayBubble arr;
arr = new ArrayBubble(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
arr.bubbleSort();
arr.display();
}
}
class ArrayBubble {
private int[] list;
private int nItems;
public ArrayBubble(int max) {
list = new int[max];
nItems = 0;
}
public void insert(int value) {
list[nItems] = value;
nItems++;
}
public void display() {
for (int j = 0; j < nItems; j++)
System.out.print(list[j] + " ");
System.out.println("");
}
public void bubbleSort() {
int out, in ;
for (out = nItems - 1; out > 1; out--)
for ( in = 0; in < out; in ++)
if (list[ in ] > list[ in +1]) swap( in , in +1);
}
public void swap(int one, int two) {
int temp = list[one];
list[one] = list[two];
list[two] = temp;
}
}
然后她要求我們修改此代碼,並使用此代碼段進行插入排序
public void insertionSort() {
int out, in ;
for (out = 1; out < nItems; out++) {
double temp = arr[out]; in = out;
while ( in > 0 && arr[ in -1] >= temp) {
arr[ in ] = a[ in -1];
-- in ;
}
arr[ in ] = temp;
}
}
這是冒泡排序的輸出..因此,如果我將其修改為插入,它應該具有相同的輸出。
77 99 44 55 22 88 11 0 66 33
0 11 22 33 44 55 66 77 88 99
流程完成。
我試圖用此代碼替換氣泡排序代碼,並將arr.bubbleSort重命名為arr.insertionSort,但仍然無法正常工作。 誰能幫忙? 謝謝。 (:
首先,您顯然需要在ArrayBubble
類中添加一個方法來實現插入排序。 由於未編譯,因此對您的代碼進行了一些修改。 有關問題,請參閱評論
public void insertionSort() {
int out, in ;
for (out = 1; out < nItems; out++) {
int temp = list[out]; in = out; // -- temp should be int, since your list is an array of int. and your array is called list, not arr
while ( in > 0 && list[ in -1] >= temp) {
list[ in ] = list[ in -1];
--in ;
}
list[ in ] = temp;
}
}
然后,你需要修改main
方法BubbleSort
調用insertionSort
:
public static void main(String[] args) {
int maxSize = 100;
ArrayBubble arr;
arr = new ArrayBubble(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
//arr.bubbleSort();
arr.insertionSort();
arr.display();
}
這個對我有用。
Here is my solution for insertion sort.
int[] input = { 5, 3, 2, 21, 70, 17, 2, 5, 19, 11 };
int[] result = insertionSort(input);
private static int[] insertionSort(int[] input) {
if (input == null) { // if array is null, print this is a null array
System.out.println("null array");
} else if (input.length < 2) { // if array is empty or has 1 element, no need to sort, will return as is
System.out.println("already sorted: ");
} else {
for (int i = 1; i < input.length; i++) { // begin with the second element and iterate until the end
for (int j = i - 1; j >= 0; j--) { // from beginning until j will be already sorted
if (input[j] > input[i]) { // if index is less then the previous, swap and continue until the beginning of the list
swapWithPrev(input, i);
i--;
}
}
}
}
return input;
}
private static void swapWithPrev(int[] input, int index) {
int temp = input[index - 1];
input[index - 1] = input[index];
input[index] = temp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.