[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sorting and unsorting

Posted on 2005-04-14
6
Medium Priority
?
326 Views
Last Modified: 2012-06-27
Hi everyone,
Im working on a mock prototype encryption program but I have some problem with a certain part of it.
My problem is that I have a string (e.g. "privatekey") which I have to first sort alphabetically to become "aeeikprtvy"
and then number them from 1 to the length of the string in an array of numbers , in this case [1,2,3,4,5,6,7,8,9,10]
after that I have to move them to their normal position in the string, in this case [6,7,4,9,1,8,2,5,3,10]. now the sorting bit is done but im still rather perplexed as to how to return the sorted array of integers to the string's original position.
thanks for your help!
PS: I hope that was clear enough to understand

0
Comment
Question by:Mytix
6 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 13787510
Hi,

It is clear...let me try to help you.

Regards
Dave
0
 
LVL 1

Expert Comment

by:vijaydogra
ID: 13787571

Is this what expected?

               Hashtable htref = new Hashtable();
            for(i = 0 ;i < str_arrary.length ; i++)
                  htref.put(str_array[i],new Integer(i).toString());
            //sorting part of the string array
            //Take the array now
            for(j = 0 ; j < int_array.length ; j++)
                  str_array[i] = htref.get(n ew Integer(int_array[j]).toString());

Hope it helps
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 600 total points
ID: 13787645
Hi,

I am going to give you some codes. Well, it is quite complex but I hope that you can understand it.

/*
 * Created on Apr 15, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.experts;

import java.util.Arrays;

/**
 * @author user
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Sort
{
      static private String test = "privatekey";
      
      public static void Unsort(char[] temp, int[] temp2)
      {
            boolean flag = false;
            
            int status = 0;
            int[] checked = new int[test.length()];
            
            for (int u = 1; u <= checked.length; u++)
            {
                  checked[u - 1] = -1;
            }            
            
            char[] x = new char[test.length()];
            x = test.toCharArray();
            
            for (int j = 0; j < x.length; j++)
            {
                  for (int k = 0; k < temp.length; k++)
                  {
                        if(temp[k] == x[j])
                        {
                              for (int l = 0; l < checked.length; l++)
                              {
                                    if (k == checked[l])
                                    {
                                          flag = true;
                                    }
                              }
                              if (flag == false)
                              {                              
                                    System.out.println(k + 1);
                                    checked[status] = k;      
                                    status++;                                    
                                    break;
                              }
                              else
                              {
                                    flag = false;
                              }
                        }            
                  }
            }
      }

      
      public static void main(String[] args)
      {
            
            char[] a = new char[test.length()];
            int[] b = new int[test.length()];
            
            for (int i = 1; i <= test.length(); i++)
            {
                  b[i - 1] = i;
            }
            
            a = test.toCharArray();
            Arrays.sort(a);
      
            Unsort(a, b);
                        
      }
}

I hope that helps.

Regards
Dave
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:Mytix
ID: 13787882
Hi guys, thanks...but that was half of what I was asking...i also half did the unsorting..but what i really wanted as a result was "aeeikprtvy" instead of numbers. how can that be done? ive tried switching the arrays but it doesnt work. thanks again for your help!
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13787929
Hi,

Try this.

/*
 * Created on Apr 15, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.experts;

import java.util.Arrays;

/**
 * @author user
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Sort
{
      static private String test = "privatekey";
      
      public static void main(String[] args)
      {
            char[] a = new char[test.length()];
            a = test.toCharArray();
            
            Arrays.sort(a);
            
            System.out.println(a);
                        
      }
}

regards
Dave
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13787970
What is exactly your input to the last part you are looking for?
Is it:
 [6,7,4,9,1,8,2,5,3,10] and "privatekey"
and the output should be
 "aeeikprtvy" ?

If not then can you please state what is the given input and what is the desired output?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question