coin-god Posted September 6, 2011 Share Posted September 6, 2011 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) Link to comment Share on other sites More sharing options...
Andrew Posted September 6, 2011 Share Posted September 6, 2011 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. Link to comment Share on other sites More sharing options...
coin-god Posted September 6, 2011 Author Share Posted September 6, 2011 I still havn't learn how to use arrays. The site looks awesome, but it's confusing me even more. Link to comment Share on other sites More sharing options...
Swoorup Posted September 6, 2011 Share Posted September 6, 2011 (edited) 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 Dim a{len(string)} as Integer'Take individual numbers to arrayFor 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 valuefor 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 jNext i'Combine the array into the stringfor 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 Edited September 11, 2011 by Swoorup Link to comment Share on other sites More sharing options...
Andrew Posted September 7, 2011 Share Posted September 7, 2011 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 //get length of inputint 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 arrayfor (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) //takes numbers array and array sizepublic 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; } Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now