我编写了一个简单的插入排序程序,但是输出不正确。
class InsertionSort{
public static void main(String h[]){
int[] a = {5,4,3,2,1};
int i,j,temp;
for(i=1;i<a.length;i++){
j = i-1;
while(i>0 && a[j] > a[i]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
for(int x=0; x<a.length;x++){
System.out.println(a[x]);
}
}
}
/*this program sort in ascending order by insertion sort */
class InsertionSort{
public static void main(String h[]){
int[] a = {100,12,31, 5, 4, 3, 2, 1 };
int i, j, temp;
for (i = 1; i < a.length; i++)
{
j = i - 1;
while (j >= 0 && a[j] > a[i] )
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i=j;
j--;
}
}
for(int x=0; x<a.length;x++){
System.out.println(a[x]);
}
}
}
/*this program sort in descending order by insertion sort */
class InsertionSort{
public static void main(String h[]){
int[] a = {100,12,31, 5, 4, 3, 2, 1 };
int i, j, temp;
for (i = 1; i < a.length; i++)
{
j = i - 1;
while (j >= 0 && a[j] < a[i] )
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
i=j;
j--;
}
}
for(int x=0; x<a.length;x++){
System.out.println(a[x]);
}
}
}
Insertion sort是用于对数组元素进行排序的另一种排序算法。此算法优于选择排序算法或冒泡排序算法。最坏情况下的时间复杂度为(n ^ 2),最坏情况下的时间复杂度为(n )。最坏情况的空间复杂度是(n),最佳情况的空间复杂度是(1)。让我们看看如何实现插入排序算法。
public int[] insertionSort(int[] list)
{
for (int i = 1; i < list.length; i++)
{
int key = list[i];
int j = i - 1;
while (j >= 0 && key < list[j])
{
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
j--;
}
}
return list;
}
虽然这里不需要循环,
在外循环的顶部,数组在元素i
下排序。您不想将i
下移到数组中。在内部循环中,通过重复切换下一个向下的元素,j
将以i
开头的新元素向下移动到排序数组中。
for (i = 1; i < a.length; i++){
for (j = i; j > 0 && a[j-1] > a[j]; j--){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
非常简单的方法
最简单的是。
public static int[] doInsertionSort(int[] input) {
int reverse;
for (int i = 1; i < input.length; i++) {
for (int j = i; j > 0; j--) {
System.out.println("compare " + input[j - 1] + " to " + input[j]);
if (input[j] < input[j - 1]) {
reverse = input[j];
System.out.println("Reverse: "+ reverse);
input[j] = input[j - 1];
input[j - 1] = reverse;
new printNumbers(input);
}
}
}
return input;
}
printNumbers(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("\n");
}
public static void insertionsort(){
Scanner sc = new Scanner(System.in);
int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
arr(input);
}
private static void printNumbers(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("\n");
}
public static void arr (int array[]){
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
printNumbers(array);
}
}
插入排序
这已经过测试并且有效
Scanner s = new Scanner(System.in);
System.out.println("Enter the nos");
num = s.nextInt();
System.out.println("Enter the "+num+" integers");
int array []= new int[num];
for(int count = 0;count<num; count++ )
{
array[count] = s.nextInt();
}
for(int i = 0;i<array.length;i++)
{
key = array[i];
j = i-1;
while(j>=0 && array[j]>key)
{
array [j+1] = array[j];
j=j-1;
}
array[j+1] = key;
}
System.out.println("Sorted array");
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}