I just wrote a big long post on this algorithm I'm working on to solve the determinant of an nxn matrix, but I got booted before I could post so it's all gone! I'm brand new to this forum.(adsbygoogle = window.adsbygoogle || []).push({});

As I was writing, I solved most of the problem, but I'm still stuck with a sorting algorithm that I can't work out.

0 3 2 0 8 4 7 0 9 0

should be sorted so that all the zeros come at the end. I would like to (but don't absolutely *need* to) preserve the order of the nonzero numbers:

3 2 8 4 7 9 0 0 0 0

The rub is that the first element (and last) is allowed to be zero. Is there some way I could avoid writing two algorithms?

if( first element is zero )

do a swap

else

so the sorting

And at that, how do you sort something like this? It doesn't look like there is a nice efficient way to do this, I have to pass over the list up to n^2 times:

loop over list or until all nonzero elements have been found

{

loop over list

{

find nonzero element that hasn't previously been found

find zero element

swap them if the nonzero element comes before the zero element

}

}

anyone got a better way to do this?

if someone wants to know more abt the determinant problem, let me know and I'll share in another thread

thanks

**Physics Forums | Science Articles, Homework Help, Discussion**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Sorting algorithm?

**Physics Forums | Science Articles, Homework Help, Discussion**