IMG

 
IMG
IMG   IMG
  Welcome to GTAForums! Be sure to check out the Grand Theft Auto V Forum.

You are not registered! (If you are, click here to login) Registering is fast, free and easy and allows you to instantly reply to any topic on GTAForums.
Why wait? Click here to register your own unique username and become part of the ever-growing community!


( Log In | Register | Revalidate Validation E-mail )
Quick Log-In:
  IMG
       
>
  Reply to this topicStart new topicStart Poll

 Simple Algorithm

 Sorting digits
 
goin-god  
Posted: Tuesday, Sep 6 2011, 18:09
Quote Post


High Roller
Group Icon
Group: $outh $ide Hoodz
Joined: Mar 18, 2007

ar.gif

Member Award




So I have a number, I have to make a function that gets this number and returns a new number in wich it's digits are sorted from highest to lowest (and second version doing lowest to highest).
I'm having a hard time thinking about how to do it.

For example 52513 would return 55321.

I've already wrote something, but imo it's to ugly and long. (And dosn't work, stopped working on it)
Could anyone help me? (It's for JAVA)
Users WebsitePMMSNPlayStation Network
  Top
 

 
Andrew  
Posted: Tuesday, Sep 6 2011, 18:14
Quote Post



Group Icon
Group: Forum Admins
Joined: Jul 21, 2003

Member Award




You're after a simple sorting algorithm, http://www.sorting-algorithms.com/ has 8 of the more common ones it also lists the peusdocode which should help you.

PM
  Top
 

 
goin-god  
Posted: Tuesday, Sep 6 2011, 18:24
Quote Post


High Roller
Group Icon
Group: $outh $ide Hoodz
Joined: Mar 18, 2007

ar.gif

Member Award




I still havn't learn how to use arrays. The site looks awesome, but it's confusing me even more. blush.gif
Users WebsitePMMSNPlayStation Network
  Top
 

 
Swoorup  
Posted: Tuesday, Sep 6 2011, 18:36
Quote Post


innovator
Group Icon
Group: Members
Joined: Oct 28, 2008

au.gif

XXXXX



Here's the VB6 code. Hope you can translate this to >> java man

BTW I used the insertion sort method cause its easier and faster when working with large numbers!

VB6 functions:

Mid(string, <starting pos> , <number of characters>
Used to store string from the middle of a string

Val(string)
Used to convert a string to number

Dim <variable> AS <data type>
Declare a variable as data type

Swap a(i),a(j)
Interchange the position of values

len
Count the number of characters in a string

CODE

Dim a{len(string)} as Integer

'Take individual numbers to array
For i= 1 to len(string)
 a(i)= mid{string,i,1}
next i

'A number Checks numbers before it and it is swapped if it bigger than the other
'So basically to check numbers before we have to start from 2nd value
for i = 2 to len (string)
 For j= 1 to i - 1
   If val{a(i)} < val{a(j)} then
      For k = j to i-1
         swap a(i), a(j)
      next k
      Exit for
   End if
 Next j
Next i

'Combine the array into the string
for I= 1 to len(strin)
 string= string+a(i)
next i


EDIT: OOPs I am sorry, use asc instead of Val, asc turns the character into ascii code

This post has been edited by Swoorup on Sunday, Sep 11 2011, 05:44
PMMSNYahoo
  Top
 

 
Andrew  
Posted: Wednesday, Sep 7 2011, 09:55
Quote Post



Group Icon
Group: Forum Admins
Joined: Jul 21, 2003

Member Award




If you post what you've got so far, we can help you improve your code and point you in the right direction.

Whilst this isn't Java it is C#, but the syntax of the two languages are practically the same.

First is to convert a number string into an array of ints
CODE


//get length of input
int strLen = input.Length;
int[] numbers = new int[strLen]; //define array of input length

//loop through the input array
//converting chars to ints
//place into numbers array
for (int i = 0; i < strLen; i++)
    {
          numbers[i] = int.Parse(input[i].ToString());
     }            


The insertion sort method is like so.
(taken from here: http://www.publicjoe.f9.co.uk/csharp/sort03.html)

CODE

//takes numbers array and array size
public static int[] sort(int[] array, int arrayLen)
       {
           int i;
           int j;
           int index;

           for (i = 1; i < arrayLen; i++)
           {
               index = array[i];
               j = i;


               while ((j > 0) && (array[j - 1] < index)) //swap < for > to reverse
               {
                   array[j] = array[j - 1];
                   j = j - 1;
               }

               array[j] = index;
           }

           return array;
       }


PM
  Top
 

 

0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)

0 Members:

Topic Options Reply to this topicStart new topicStart Poll
Search topic for posted by (exact match)



 
IMG IMG