Sorting algorithm

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys, I have a simple Bubble Sort:

/**
 * Bubble Sort Elements
 */
void long_sort_directories(struct long_name * elements, int nr_elements){


	/**
	 * Sort Directory List using Bubble Sort
	 */
	struct long_name temp;

	int l, n;

	for (l = 0; l < nr_elements - 1; l++) {
		for (n = 0; n < nr_elements - 1; n++) {

			if (strcmp(elements[n].name, elements[n + 1].name) > 0) {

				strcpy(temp.name, elements[n].name);
				strcpy(elements[n].name, elements[n + 1].name);
				strcpy(elements[n + 1].name, temp.name);
			}
		}
    }
}

When it sorts the lists it will sort as follows:

 

test8

test80

test81

test82

test 9

test90

test91

test92

test93

 

I  would prefer to have it:

test8

test9

test80

test81

test82

test90

 

Would there be a simple way to alter the function?

 

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The simplest way would be to rename the first ten elements from "test0..test9" to "test00 .. test09".   Manually, if only 10 entries.

 

  There is always a programming solution of loading "testnnn" into a string, remove the "test", convert the remainder to an integer and/or get the length of the string "testnnn" and if less than 6, do this and that.  But whatever is generating the test message enumeration "testnnn" should be changed to include leading zeros in the post-fix counter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if it's as numbers you need to sort them then, sort them as numbers not strings, else do as #2 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there any rule for the format?  Is it always 4 alpha chars, followed by some numeric digits?

Or perhaps simply some arbitrary length alpha chars appended with numerics?

In either case, "convert" everything to a common sortable format, before doing the actual sorting (so the conversions only need done once).

You can sort on these & keep a tracking version of the original strings...so when finished, you can spit out the non-converted strings, like magic. 

 

here's some tips:

http://www.davekoelle.com/alphanum.html

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Fri. Oct 25, 2019 - 12:46 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:
Would there be a simple way to alter the function?
Stop using strcmp() then. But if you are doing sorting in C you do know about qsort don't you? 

 

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#gafd4bf2faec43342e7ad3d2ab37bac1fe

 

You still face the same issue as the last parameter in qsort() is a function pointer to your own comparison function and while it would have been tempting to use strcmp() again you will end up with the same result.

 

In either case you *might* be able to temporarily massage the input data (only to the comparison) by inserting leading 0's to pad any numeric. An alternative might be to bring strlen() into play somehow (clearly "test8" is short than "test82")

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Fianawarrior wrote:
Would there be a simple way to alter the function?
Stop using strcmp() then. But if you are doing sorting in C you do know about qsort don't you? 

 

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#gafd4bf2faec43342e7ad3d2ab37bac1fe

 

You still face the same issue as the last parameter in qsort() is a function pointer to your own comparison function and while it would have been tempting to use strcmp() again you will end up with the same result.

 

In either case you *might* be able to temporarily massage the input data (only to the comparison) by inserting leading 0's to pad any numeric. An alternative might be to bring strlen() into play somehow (clearly "test8" is short than "test82")

 

I've been thinking there may be a way to insert strlen in to the mix.