This was written by Sua Thov over on the App Inventor Coffee Shop (Google Groups).
This is a complete example of Sorting Numbers or Words along with functions to Make Upper and Make Lower and Comparing Two Strings. While these functions have since been added to App Inventor this is still good programming.
The Source Code: https://sites.google.com/site/blocks123/sorting-1/SortListDemo.zip
The Bubble Sort:
From Wikipedia
Like most things in App Inventor we start with a List, of things we want to sort.
Now this list can be filled with numbers or text, or a combination of the two.
First we check to see if the list has only numbers or not. As sorting text is harder and takes longer to do.
Now we are going to go through the list and compare it to the item following it.
For Numbers it's a simple 'is a > b' (or 'is a < b' for reverse order)
For Text we need to call a procedure to compare two strings (it's covered below)
Now if a is larger then b we swab them in the list.
and then we do it again and again until we can go through the list without any swaps happening.
First we convert the strings to Upper Case (see section below) so Aa and aa are equal in the eyes of this routine.
If you want to change this so that Aa is not equal to aa then remove the calls 'ToUpper' and also make up a new Text to replace 'alphaUpperCaseSet' currently
' ~`!@#$%^&*()_-+=[]\{}|;':",./<>?0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' to '~`!@#$%^&*()_-+=[]\{}|;':",./<>?0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
The meat of this routine is comparing where each letter in each string is in the 'alphaUpperCaseSet' string. So 'A'(42) - 'A'(42) = 0 and are equal
while 'A'(42) - 'B'(43) = -1 so 'A' comes before 'B'
This is a complete example of Sorting Numbers or Words along with functions to Make Upper and Make Lower and Comparing Two Strings. While these functions have since been added to App Inventor this is still good programming.
The Source Code: https://sites.google.com/site/blocks123/sorting-1/SortListDemo.zip
The Bubble Sort:
From Wikipedia
Bubble sort
is a simple
sorting algorithm
that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and
swapping
them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a
comparison sort
.
Like most things in App Inventor we start with a List, of things we want to sort.
Now this list can be filled with numbers or text, or a combination of the two.
First we check to see if the list has only numbers or not. As sorting text is harder and takes longer to do.
Now we are going to go through the list and compare it to the item following it.
For Numbers it's a simple 'is a > b' (or 'is a < b' for reverse order)
For Text we need to call a procedure to compare two strings (it's covered below)
Now if a is larger then b we swab them in the list.
and then we do it again and again until we can go through the list without any swaps happening.
Comparing Two Strings
Here is where sorting Text takes so much more time then Numbers.First we convert the strings to Upper Case (see section below) so Aa and aa are equal in the eyes of this routine.
If you want to change this so that Aa is not equal to aa then remove the calls 'ToUpper' and also make up a new Text to replace 'alphaUpperCaseSet' currently
' ~`!@#$%^&*()_-+=[]\{}|;':",./<>?0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' to '~`!@#$%^&*()_-+=[]\{}|;':",./<>?0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
(see next photo for 'alphaUpperCaseSet')
The meat of this routine is comparing where each letter in each string is in the 'alphaUpperCaseSet' string. So 'A'(42) - 'A'(42) = 0 and are equal
while 'A'(42) - 'B'(43) = -1 so 'A' comes before 'B'
댓글 없음:
댓글 쓰기