如何在不使用Array.sort:Java的情况下按字母顺序对数组进行排序 [英] How to sort array in alphabetical order without using Array.sort:Java

查看:79
本文介绍了如何在不使用Array.sort:Java的情况下按字母顺序对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中按字母顺序对数组的值进行排序.我被困在再次遍历数组并获取输出的过程中.我的意图是:遍历单词数组,找到最大的字符串(按字典顺序),找到后,将单词插入sortedWords数组的末尾,将sortedArray的索引向左移动一个位置,通过删除已经找到的单词来减少原始数组,再次循环以查找下一个单词....

I want to sort values of an array in alphabetical order in Java.I am stuck at going over the array again and get the output. My intention is to : Go over the words array, Find the largest string (lexicographically), Once it is found, insert the word at the end of the sortedWords array, Shift the sortedArray's index by one position to the left, Reduce the original array by removing the word already found, Loop again to find the next word....

感谢您的帮助.

以下是我到目前为止所做的.

Following is what I have done so far.

公共类主要{

public static void main(String[] args) {
    String[] words = {"bob","alice","keith","zoe","sarah","gary"};
    String[] sortedWords = new String[words.length];

    // Copy of the original array
    for(int i = 0; i < sortedWords.length;i++){
        sortedWords[i]= words[i];
    }


    for (int i = 0; i < words.length - 1; i++) {
    int currentSize = words.length;
    int position = 0;
    boolean found = false;
    while (position < currentSize && !found) {
        if (words[i].equals(largestAlphabetically(words))) {
            found = true;

        } else {
            position++;
        }
    }

        if(found){
            insertAtEnd(words,largestAlphabetically(words));
            shiftLeft(sortedWords,i);
            shorterArray(words);

        }

    }
    for(int i = 0;i < sortedWords.length;i++){
        System.out.println(sortedWords[i]);
    }

}


/**
 * This method inserts the largest string lexicographically at the end of the array
 * @param words
 * @param wordToInsert
 * @return an array with string at the end
 */

public static String [] insertAtEnd(String[] words, String wordToInsert) {

    for (int i = 0; i < words.length; i++) {
        int currentSize = words.length - 1;
        wordToInsert = largestAlphabetically(words);
        if (currentSize < words.length) {
            currentSize++;
            words[currentSize - 1] = wordToInsert;
        }
    }
    return words;
}

/**
 * This method determines the largest string in an array
 * @param words
 * @return largest string lexicographically
 */
public static String largestAlphabetically(String[] words) {
    String searchedValue = words[0];
    for (int i = 0; i < words.length; i++) {

        for (int j = 0; j < words.length; j++) {
            if (words[i].compareToIgnoreCase(words[j]) < 0) {
                searchedValue = words[j];
            }
        }
    }
    return searchedValue;
}

/**
 * To shift the array index to the left
 * @param dest
 * @param from
 */

public static void shiftLeft(String[] dest, int from) {
    for (int i = from + 1; i < dest.length; i++) {
        dest[i - 1] = dest[i];
    }
    dest[dest.length - 1] = dest[0];
}

/**
 * Remove the largest word from a string while maintaining the order of the array
 * @param words
 * @return return a shorter array
 */
public static String [] shorterArray(String[] words) {
    String [] shorterArray = new String[words.length];
    int currentSize = words.length;
    String searchedValue = largestAlphabetically(words);
    int position = 0;
    boolean found = false;
    while (position < currentSize && !found) {
        if (words[position] == searchedValue) {
            found = true;

        } else {
            position++;
        }
    }
    if (found) {
        for (int i = position + 1; i < currentSize; i++) {
            words[i - 1] = words[i];

        }
        currentSize--;
        shorterArray = words;
    }

    return shorterArray;

}

}

推荐答案

简单的实现可以像这样:

Simple implementation can be like this:

String[] words = {"bob","alice","keith","zoe","sarah","gary"};

boolean isSwapped = false;
do {
    isSwapped = false;
    for(int i=0;i<words.length-1;i++){
        if(words[i].compareTo(words[i+1])>0){
            String temp = words[i+1];
            words[i+1] = words[i];
            words[i] = temp;
            isSwapped = true;
        }
    }
}while((isSwapped));

这篇关于如何在不使用Array.sort:Java的情况下按字母顺序对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆