使用插入排序方法对字符串数据数组进行排序

时间:2014-03-31 23:45:08

标签: java arrays sorting insertion-sort

我在使用插入排序字符串数组时遇到了问题。

编译以下代码时:

public class Project1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String names[]=new String[5];
        int size=names.length;
        System.out.println("Enter the 5 car manufacturers: ");
        //Load Array
        for (int i = 0; i < 5; i++) {
            names[i] = input.nextLine();            
        }

        //Print descending order list
        String[] descSort;
        descSort=bubbleSortDesc(names);
        System.out.println("Car manufacturers listed sorted in descending order (via BubbleSort): ");
        for (int x=0; x < names.length; x++) {
            System.out.println(names[x]);
        }

        //Print ascending order list
        insertionSortAsc(names, size);
        System.out.println("Car manufacturers listed sorted in ascending order (via InsertionSort): ");
        for (int z=0; z < names.length; z++) {
            System.out.println(names[z]);
        }
    }¨

    public static String[] bubbleSortDesc(String[] names) {
        String temp;
        int passNum, i, result;
        for (passNum=1; passNum <= 4; passNum++) {
            for (i = 0; i<=(4-passNum); i++) {
                result=names[i].compareToIgnoreCase(names[i+1]);
                if(result<0) {
                    temp=names[i];
                    names[i]=names[i+1];
                    names[i+1]=temp;
                }
            }
        }
        return names;
    }

    public static void insertionSortAsc(String[] names, int i) {
        String temp = names[i];
        int j = i-1;
        while (j >= 0 && names[j].compareToIgnoreCase(temp) > 0) {
            names[j+1]=names[j];
            j--;
        }
        names[j+1]=temp; 
    }

    public static void insertionSort(String[] names, int n) {
        for(int i = 1; i<n; i++) {
            insertionSortAsc(names, i);
        }
    }
}

它给了我错误:

cannot find symbol- method insert(java.lang.String[], int)

我怀疑它与我们被告知使用我们的书作为代码的参考这一事实有关,但本书只处理int类型的数据排序,并且没有排序字符串数据的示例。

感谢任何帮助。

编辑:修复错误后,程序编译并执行但输入数据后崩溃并给我以下错误

java.lang.ArrayIndexOutofBoundsException:
5

此错误突出显示第String temp = names[i]

3 个答案:

答案 0 :(得分:0)

您尚未定义名为insert的方法。

答案 1 :(得分:0)

这将按您的方式运作:

public static void insertionSortAsc(String[] names, int n)
{
    for(int i = 1; i<n; i++)
    {
        insert(names, i);
    }
}

public static void insert(String[] names, int i)
{
    String temp = names[i];
    int j = i - 1;

    while (j >= 0 && names[j].compareToIgnoreCase(temp)  > 0)
    {
        names[j + 1]= names[j];
        j--;
    }
    names[j + 1] = temp;
}

答案 2 :(得分:0)

import { Component } from '@angular/core';
import { ActionSheetController } from '@ionic/angular';

@Component({
  selector: 'action-sheet-example',
  templateUrl: 'action-sheet-example.html',
  styleUrls: ['./action-sheet-example.css'],
})
export class ActionSheetExample {

  constructor(public actionSheetController: ActionSheetController) {}

  async presentActionSheet() {
    const actionSheet = await this.actionSheetController.create({
      header: 'Albums',
      cssClass: 'my-custom-class',
      buttons: [
        {
          text: 'Delete',
          role: 'destructive',
          icon: 'trash',
          handler: () => {
            console.log('Delete clicked');
          }
        }, 
        {
          text: 'Share',
          icon: 'share',
          handler: () => {
            console.log('Share clicked');
          }
        }, 
        {
          text: 'Play (open modal)',
          icon: 'caret-forward-circle',
          handler: () => {
            console.log('Play clicked');
          }
        }, 
        {
          text: 'Favorite',
          icon: 'heart',
          handler: () => {
            console.log('Favorite clicked');
          }
        }, 
        {
          text: 'Cancel',
          icon: 'close',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });
    
    await actionSheet.present();
  }

}