Java 插入排序?
所以我们的老师给了我们这个气泡排序的代码Java 插入排序?,java,arrays,sorting,data-structures,Java,Arrays,Sorting,Data Structures,所以我们的老师给了我们这个气泡排序的代码 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
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;
}
}
进程已完成
我试图用此代码替换冒泡排序代码,并将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;
}
}
它适合我。这是我的插入排序解决方案。
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;
}
int[]输入={5,3,2,21,70,17,2,5,19,11};
int[]结果=插入排序(输入);
私有静态int[]插入排序(int[]输入){
如果(input==null){//如果数组为null,则打印这是一个null数组
System.out.println(“空数组”);
}else if(input.length<2){//如果数组为空或有1个元素,则无需排序,将按原样返回
System.out.println(“已排序:”);
}否则{
对于(inti=1;i=0;j--){//从开始到j都将被排序
如果(input[j]>input[i]){//如果索引小于上一个,则交换并继续,直到列表开始
swapWithPrev(输入,i);
我--;
}
}
}
}
返回输入;
}
私有静态void swapWithPrev(int[]输入,int索引){
int temp=输入[索引-1];
输入[索引-1]=输入[索引];
输入[索引]=温度;
}
@Bombe,所有的家庭作业问题都不值得投票表决……她显然已经付出了努力,并在没有产生正确结果时寻求帮助。如果你能指定“它仍然不起作用”,那会很有帮助。你收到了什么错误消息?@Codebender我认为他们确实应该结束讨论;他们的老师有报酬来回答这类问题。我同意Bombe的观点。你的老师提供的代码写得非常好。你只需更改一些变量名并将其添加到你的ArrayBubble
类中。花点时间来做,练习一次nd学习。@gefei它不排序它只显示相同的东西77994455228811 0 66 33 7799445522 88 11 0 66 33
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;
}
}
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;
}