Writing a program that, if given an array of characters, will output array that has the indices in order from largest to smallest (java)

huangapple go评论73阅读模式
英文:

Writing a program that, if given an array of characters, will output array that has the indices in order from largest to smallest (java)

问题

public static void main(String[]args){

        char[] a ={'f','a','z','v','t','u','i','j','o','c'};
        int[] b = new int[a.length];
      
        // Sort a
        for(int i = 0; i <= a.length - 2; i++){
            
            int maxindex = i;
            
            for(int j = maxindex + 1; j < a.length; j++){
                if(a[j] > a[maxindex]){
                    maxindex = j;
                }
            }
            // swap
            char temp = a[i];
            a[i] = a[maxindex];
            a[maxindex] = temp;
            
            b[i] = maxindex; // Store the index in the corresponding position of array b
        }
        for(int i = 0; i < a.length; i++){
            System.out.print(a[i] + " ");
        }
    }
英文:

So I'm working through a problem that if I have an array of characters it wants me to arrange them in order from largest to smallest but by the index not the actual letter. so ex, a[] = { &#39;a&#39;, &#39;z&#39;, &#39;p&#39;} the output array would be like `b[] = {1 , 2, 0}

I understand how to sort the array of characters into descending order but now I'm confused on how to relate the two arrays together. I'm thinking I need to copy the original array into another array and then compare them someway. But im confused on the steps i need to take to get there. My sorting code is below just for reference. if anyone can help me out that would be great. Thanks

public static void main(String[]args){

       char[] a ={&#39;f&#39;,&#39;a&#39;,&#39;z&#39;,&#39;v&#39;,&#39;t&#39;,&#39;u&#39;,&#39;i&#39;,&#39;j&#39;,&#39;o&#39;,&#39;c&#39;};
       int[] b ;
     
     
     
       // Sort a
       for(int i= 0; i &lt;= a.length-2;i++){
           
           int maxindex = i;
           
           for(int j = maxindex+1; j&lt;a.length; j++){
               if(a[j]&gt;a[maxindex]){
                   maxindex = j;
               }
           }
           // swap
           char temp = a[i];
           a[i] = a[maxindex];
           a[maxindex] = temp;
       }
       for(int i = 0; i &lt; a.length; i++){
           System.out.print(a[i] + &quot; &quot;);
       }
   }

答案1

得分: 0

// 你好!比我想象的要棘手一些。下面是我所做的:

// 1. 从原数组中制作一份副本。
// 2. 对原数组进行排序。
// 3. 比较两个数组,以获取具有索引的数组。

// 就像你想的那样。我认为你可以使用其他类型的数据结构来简化代码,但如果这是为了练习Java,那也没问题。

char[] sorted = {'z', 'f', 'z', 'v', 't', 'u', 'i', 'j', 'o', 'k', 'b'};

// 让我们制作一个副本
char[] copy = sorted.clone();

// 使用与'sorted'数组相同大小的整数数组初始化'b'
int[] b = new int[sorted.length];

// 对原始数组进行排序(这是你的代码)
for (int i = 0; i <= sorted.length - 2; i++) {
    
    int maxindex = i;
    
    for (int j = maxindex + 1; j < sorted.length; j++) {
        if (sorted[j] > sorted[maxindex]) {
            maxindex = j;
        }
    }
    // 交换
    char temp = sorted[i];
    sorted[i] = sorted[maxindex];
    sorted[maxindex] = temp;
}

// 看看我们得到了什么
for (int i = 0; i < sorted.length; i++) {
    System.out.print(sorted[i] + " ");
}

// 现在比较原始和排序后的数组,然后获取索引数组
for (int i = 0; i < sorted.length; i++) {
    for (int k = 0; k < copy.length; k++) {
        if ((copy[k] == sorted[i] && i == 0) || (copy[k] == sorted[i] && k != b[i - 1])) {
            b[i] = k;
            break;
        }
    }
}

System.out.print("\n");

// 看看索引数组
for (int i = 0; i < b.length; i++) {
    System.out.print(b[i] + " ");
}
英文:

<!-- language: lang-java -->
Hi! It was a bit more challenging than I thought. Here is what I did:

  1. Make a copy from the original array.
  2. Sort the original array.
  3. Compare the two arrays so you can get the array with indexes

It's just like you thought. I think you could simplify the code using other types of structures, but it's fine if it is to practice java

char[] sorted = {&#39;z&#39;,&#39;f&#39;,&#39;z&#39;,&#39;v&#39;,&#39;t&#39;,&#39;u&#39;,&#39;i&#39;,&#39;j&#39;,&#39;o&#39;,&#39;k&#39;,&#39;b&#39;}; 
// Let&#39;s copy it
char[] copy = sorted.clone();
// initialize b as an array of integer with the same size as &#39;sorted&#39; array
int[] b= new int[sorted.length];
// Sort your original array (this is your code)
for(int i= 0; i &lt;= sorted.length-2;i++){
int maxindex = i;
for(int j = maxindex+1; j&lt;sorted.length; j++){
if(sorted[j]&gt;sorted[maxindex]){
maxindex = j;
}
}
// swap
char temp = sorted[i];
sorted[i] = sorted[maxindex];
sorted[maxindex] = temp;
}
// let&#39;s see what we got
for(int i = 0; i &lt; sorted.length; i++){
System.out.print(sorted[i] + &quot; &quot;);
}
// now let&#39;s compare the original vs the sorted and get the indexes array
for(int i = 0; i &lt; sorted.length; i++){
for(int k = 0; k &lt; copy.length; k++){
if ((copy[k] == sorted[i] &amp;&amp; i == 0)  || (copy[k] == sorted[i] &amp;&amp; k != b[i - 1]) ) {
b[i] = k; 
break;
}				  
}
}
System.out.print(&quot;\n&quot;);
// let&#39;s see the indexes array
for(int i = 0; i &lt; b.length; i++){
System.out.print(b[i] + &quot; &quot;);
}

huangapple
  • 本文由 发表于 2020年10月5日 04:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64199600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定