sanjibnanda Active Member Licensed User Longtime User Nov 24, 2012 #1 HTML: Hi , How can i shuffle string of array alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; TO SOME RANDOM STATE LIKE alphabets = {"C", "A", "I", "D", "G", "J", "H", "H", "B", "F"};
HTML: Hi , How can i shuffle string of array alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; TO SOME RANDOM STATE LIKE alphabets = {"C", "A", "I", "D", "G", "J", "H", "H", "B", "F"};
M MLDev Active Member Licensed User Longtime User Nov 24, 2012 #2 Code: ' Knuth's Algorithm P Dim n As Int = alphabets.Length Dim r As Int Dim t As String For i = 0 To n - 1 r = Rnd(i, n) t = alphabets(i) alphabets(i) = alphabets(r) alphabets(r) = t Next Upvote 0
Code: ' Knuth's Algorithm P Dim n As Int = alphabets.Length Dim r As Int Dim t As String For i = 0 To n - 1 r = Rnd(i, n) t = alphabets(i) alphabets(i) = alphabets(r) alphabets(r) = t Next
sanjibnanda Active Member Licensed User Longtime User Nov 24, 2012 #3 shuffle Array As string MLDev said: Code: ' Knuth's Algorithm P Dim n As Int = alphabets.Length Dim r As Int Dim t As String For i = 0 To n - 1 r = Rnd(i, n) t = alphabets(i) alphabets(i) = alphabets(r) alphabets(r) = t Next Click to expand... Thanks for hint MLdev ... i converted it to sub , may be helpful for some Newbie Code: Sub ShuffleArray(arr() As String) Dim n As Int = arr.Length-1 Dim r As Int Dim t As String For i = 0 To n - 1 r = Rnd(i, n) t = arr(i) arr(i) = arr(r) arr(r) = t Next Return arr End Sub How to use Code: ShuffleArray(YourArr) B4A forum is wonderful as long as people like you live there... it worked like a charm...thanks again to you and Knuth's Algorithm P regards sanjib Upvote 0
shuffle Array As string MLDev said: Code: ' Knuth's Algorithm P Dim n As Int = alphabets.Length Dim r As Int Dim t As String For i = 0 To n - 1 r = Rnd(i, n) t = alphabets(i) alphabets(i) = alphabets(r) alphabets(r) = t Next Click to expand... Thanks for hint MLdev ... i converted it to sub , may be helpful for some Newbie Code: Sub ShuffleArray(arr() As String) Dim n As Int = arr.Length-1 Dim r As Int Dim t As String For i = 0 To n - 1 r = Rnd(i, n) t = arr(i) arr(i) = arr(r) arr(r) = t Next Return arr End Sub How to use Code: ShuffleArray(YourArr) B4A forum is wonderful as long as people like you live there... it worked like a charm...thanks again to you and Knuth's Algorithm P regards sanjib
M MLDev Active Member Licensed User Longtime User Nov 24, 2012 #4 With this line in your sub: Code: Dim n As Int = arr.Length-1 The last element in the array will always be the same. It should be: Code: Dim n As Int = arr.Length EDIT And this line in my code: Code: For i = 0 To n - 1 should be: Code: For i = 0 To n - 2 :signOops: Last edited: Nov 24, 2012 Upvote 0
With this line in your sub: Code: Dim n As Int = arr.Length-1 The last element in the array will always be the same. It should be: Code: Dim n As Int = arr.Length EDIT And this line in my code: Code: For i = 0 To n - 1 should be: Code: For i = 0 To n - 2 :signOops:
sanjibnanda Active Member Licensed User Longtime User Nov 24, 2012 #5 Acknowledged with regards Upvote 0
DrownedBat Member Licensed User Longtime User Oct 14, 2015 #6 Any way to reverse this process? take alphabets = {"C", "A", "I", "D", "G", "J", "H", "H", "B", "F"}; and arrange it back into alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; Upvote 0
Any way to reverse this process? take alphabets = {"C", "A", "I", "D", "G", "J", "H", "H", "B", "F"}; and arrange it back into alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
S sorex Expert Licensed User Longtime User Oct 14, 2015 #7 I guess the easiest would be to loop through the elements and put them into a list, sort the list, write it back to the array. or you can write your own sort routine on the array. Upvote 0
I guess the easiest would be to loop through the elements and put them into a list, sort the list, write it back to the array. or you can write your own sort routine on the array.
J James Chamblin Active Member Licensed User Longtime User Oct 14, 2015 #8 DrownedBat said: Any way to reverse this process? take alphabets = {"C", "A", "I", "D", "G", "J", "H", "H", "B", "F"}; and arrange it back into alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; Click to expand... Check out this thread https://www.b4x.com/android/forum/threads/sorting-algorithms-teaching-with-basic4android.8548/ Upvote 0
DrownedBat said: Any way to reverse this process? take alphabets = {"C", "A", "I", "D", "G", "J", "H", "H", "B", "F"}; and arrange it back into alphabets = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}; Click to expand... Check out this thread https://www.b4x.com/android/forum/threads/sorting-algorithms-teaching-with-basic4android.8548/
Erel B4X founder Staff member Licensed User Longtime User Oct 14, 2015 #9 Use a list instead of an array and call List.Sort to sort the items. Upvote 0