Difference between revisions of "Data Sorting in VB6"
From Free Knowledge Base- The DUCK Project: information for everyone
Line 1: | Line 1: | ||
== Sorting Algorithms == | == Sorting Algorithms == | ||
− | # Bogosort | + | {| |
− | # Bubble sort | + | |- |
− | # Cocktail sort | + | |# Bogosort |
− | # Comb sort | + | |- |
− | # Selection sort | + | |# Bubble sort |
− | # Insertion sort | + | |# Very fast for lists that are almost sorted, slow sorting anything else. |
− | # Bucket sort | + | |- |
− | # Counting sort | + | |# Cocktail sort |
− | # Heapsort | + | |- |
− | # Smoothsort | + | |# Comb sort |
− | # Merge sort | + | |- |
− | # Quicksort | + | |# Selection sort |
− | # Binary tree sort | + | |- |
− | # Pigeonhole sort | + | |# Insertion sort |
− | # Radix sort | + | |- |
− | # Shell sort | + | |# Bucket sort |
+ | |- | ||
+ | |# Counting sort | ||
+ | |- | ||
+ | |# Heapsort | ||
+ | |- | ||
+ | |# Smoothsort | ||
+ | |- | ||
+ | |# Merge sort | ||
+ | |- | ||
+ | |# Quicksort | ||
+ | |- | ||
+ | |# Binary tree sort | ||
+ | |- | ||
+ | |# Pigeonhole sort | ||
+ | |- | ||
+ | |# Radix sort | ||
+ | |- | ||
+ | |# Shell sort | ||
+ | |} | ||
== Sorts -w- Examples == | == Sorts -w- Examples == | ||
− | === quicksort === | + | === quicksort (and remove duplicates) === |
This program sorts a list using a standard quicksort algorithm. See the code for how the algorithm works. | This program sorts a list using a standard quicksort algorithm. See the code for how the algorithm works. |
Revision as of 10:02, 29 January 2008
Contents
Sorting Algorithms
# Bogosort | |
# Bubble sort | # Very fast for lists that are almost sorted, slow sorting anything else. |
# Cocktail sort | |
# Comb sort | |
# Selection sort | |
# Insertion sort | |
# Bucket sort | |
# Counting sort | |
# Heapsort | |
# Smoothsort | |
# Merge sort | |
# Quicksort | |
# Binary tree sort | |
# Pigeonhole sort | |
# Radix sort | |
# Shell sort |
Sorts -w- Examples
quicksort (and remove duplicates)
This program sorts a list using a standard quicksort algorithm. See the code for how the algorithm works.
Then if you have the Remove Dupes option selected, the program removes duplicates. It scans through the sorted array, comparing each item to the one before it. If the item is different, it copies the item into a new array.
Public Function RemoveDups(strings() As String) As String() Dim old_i As Integer Dim last_i As Integer Dim result() As String ' Make the result array. ReDim result(1 To UBound(strings)) ' Copy the first item into the result array. result(1) = strings(1) ' Copy the other items last_i = 1 For old_i = 2 To UBound(strings) If result(last_i) <> strings(old_i) Then last_i = last_i + 1 result(last_i) = strings(old_i) End If Next old_i ' Remove unused entries from the result array. ReDim Preserve result(1 To last_i) ' Return the result array. RemoveDups = result End Function
Quicksort is a fine general purpose sorting algorithm. For different kinds of data, however, it may not be the fastest or the best. For example, it has trouble sorting lists that contain many duplicate values and the algorithm countingsort is much faster at sorting numeric data.
- Keep in mind that you cannot use countingsort to sort strings.