将从数组接收的字符按字母顺序放置而不使用排序功能? [英] Put characters received from an array in alphabetical order without using sorting functions?

查看:71
本文介绍了将从数组接收的字符按字母顺序放置而不使用排序功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我在Java中创建了一种算法,该算法从字符串中提取字符并计数其频率.现在,我需要按字母顺序打印它.

So, basically, I created an algorithm in java that takes characters from a string and counts its frequencies. Now, I need it to be printed in alphabetical order.

例如: 频率:3 d频率:1 l频率:5

For example : A freq: 3 d freq: 1 l freq: 5

有什么建议吗?到目前为止,这就是我所拥有的.

Any suggestions? Here's what I have thus far.

     int[] charCounts(String userSort) {
        int[] counts = new int[256];
        char[] c = userSort.toCharArray();
        for (int i=0;i<c.length;++i) {
            counts[c[i]]++;
        }
        return counts;
}

推荐答案

首先,您要对提供的String中的字符进行排序,而无需使用任何API排序方法的简单方法就是利用两个 for 循环.当然,您需要将提供的字符串分解为字符数组.假设提供的字符串为"This is my string to sort":

First you want to sort the characters within your supplied String and a simple way to do that without using any API sorting methods is to utilize two for loops. Of course you will need to break down the supplied string into a character array. Let's assume the supplied string is "This is my string to sort":

String suppliedString = "This is my string to sort";
char[] charArray = suppliedString.toCharArray();

现在使用两个for循环遍历Character Array并操纵该Array的元素,以使最小的字符值朝着开头,而较大的值朝着结尾.这种排序方式称为 气泡排序 它是这样的:

Now use two for loops to iterate through the Character Array and manipulate the elements of that Array to bring the least character values towards the beginning and the greater values progressively to the end. This type of sorting is called a Bubble Sort and it goes something like this:

注意::是的.在下面的代码中,有很多注释说明了发生的情况.如此之多以至于它是彻头彻尾的 混乱.那就是编辑器的优点,您可以轻松删除 如果您不想要它们的话.

Note: Yes...there are a lot of comments within the following code explaining what is going on. So much so that it's downright cluttering. That's what's so good about editors, you can easily delete them if you don't want them.

// The supplied String to sort.
String suppliedString = "this is my string to sort";
// Remove all whitespaces. We don't need them for 
// this excercise since our goal is to sort and 
// get character occurrences. If you want to also
// process whitespaces then comment the code line
// below.
suppliedString = suppliedString.replace(" ", "");

// Convert the supplied string to a character array.
char[] charArray = suppliedString.toCharArray();
// Declare a Character variable to hold the current
// Character Array element value being processed.
char tempChar;
// Iterate through the character array with two
// FOR loops so as to create a string which will
// hold the least character values to the greatest
// character values.
for (int i = 0; i < charArray.length; i++) {
    for (int j = 0; j < charArray.length; j++) {
        // Is the current Array element value in 
        // charArray[i] less than the what is in
        // the current Array element for charArray[j]?
        if (charArray[i] < charArray[j]) {
            // Yes it is...
            // Hold our current character element value.
            tempChar = charArray[i];
            // Now make the Array element at index i hold
            // what is in Array element at index j.
            charArray[i] = charArray[j];
            // Make the Array element at index j hold what
            // was originally in the Array element at index i.
            charArray[j] = tempChar;
        }
        // No it's not so let's continue iterations through 
        // the character array using the index place-holder 
        // j to see if there are still more character Array 
        // element values less than what is currently in the 
        // Character Array index place-holder i location.
    }
    // continue iterations through the character array 
    // using the index place-holder i to see if there 
    // are still more character Array element values less
    // that what might be in the Character Array index place
    // -holder j location.
}

//==============================================
// For your project you don't need this little
// section. I just added it so you can see what 
// the sort looks like.
// Now use yet another FOR loop to convert the 
// the sorted Character Array (charArray[]) back
// to a sorted string.
// Declare and initialize a String variable to 
// Null String (""). This variable will hold the
// new Sorted String.
String sortedString = "";
for (int i = 0; i < charArray.length; i++) {
    sortedString+= charArray[i];
}

// Display the sorted String. If you don't
// want spaces in your sort then use: 
// System.out.println(sortedString.trim());
// Spaces have the least value (32) so they
// will almost always be at the beginning of
// the sorted string.
System.out.println("Sorted String: -->   " + sortedString + "\n");
//==============================================

// Now that the Character Array is sorted let's
// use yet another couple FOR loops to figure out
// the occurrences of each character. We'll use our 
// same String variable (sortedString) to hold our 
// display text to console. (Note: There's a lot of 
// ways to do this sort of thing in Java)
int counter; // counter used to keep track of char occurrences.
sortedString = "";
for (int i = 0; i < charArray.length; i++) {
    counter = 0; // new character. Make sure counter is zeroed
    // Iterate through the entire array and count
    // those that are the same.   
    for (int j = 0; j < charArray.length; j++) {
        if (charArray[i] == charArray[j]) {
            counter++;
        }
    }
    // Make sure we don't place duplicate character/frequencies
    // into the string we're creating.
    if (!sortedString.contains("Char: " + charArray[i])) {
        // Add the current character and occurrence
        // to our string variable.
        if (sortedString.equals("")) {
            sortedString+= "Char: " + charArray[i] + " - Freq: " + counter; 
        } 
        else {
            sortedString+= " || Char: " + charArray[i] + " - Freq: " + counter; 
        }
    }
}
// Display the sorted characters and their occurrences.
System.out.println(sortedString);

是的,该代码中有很多 for 循环.一旦理解了代码,就可以删除所有注释.完成后,您会发现确实没有太多代码可以完成此任务.

Yup, a lot of for loops in that code. Once you understand the code then delete all the commenting if you like. Once you do you'll see that there really isn't too much code to accomplish this task.

这篇关于将从数组接收的字符按字母顺序放置而不使用排序功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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