Sorting (1 Viewer)

Lundy

Banned
Joined
Sep 2, 2003
Messages
2,512
Location
pepperland
Gender
Female
HSC
2003
I have some confusion about the selection and insertion sorts:

I've read the definitions of both in the text, but my teacher has been teaching them to us vice versa: the selection as insertion and insertion as selection. The notes I was given contradict the textbook as to which is which.

Could someone give me the correct definition of each?

thanks
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
Selection:

Two lists are maintained, a sorted and an unsorted list. Initially, the sorted list has 0 items in it, and the unsorted list has all the items in it. A linear search is then conducted in the unsorted list, and the highest/lowest value is found (depending on whether you are doing descending/ascending sort).
This item is then placed at the beginning of the unsorted list, and this item is now in order. So the sorted list size increases by one after each pass, and the unsorted list size decreases by one.
A linear search, then swap process is continued until all items are in order.


Insertion:

Two lists are maintained again.
The first item in the unsorted list is taken as being in order. The next item in the unsorted list is then looked at, and it is placed in its correct position in the sorted list. This is done by shuffling all the other items either up/down one place, and inserting the next item into the gap created.
After each pass, the sorted list size increases by one, the unsorted decreases by one.

Some comparisons:
As selection sort has a MUCH larger number of swaps (swaps take more processing power than a shuffle), this method is slower than an insertion sort FOR LARGE LISTS of data. The difference is so small for small lists that the overhead created by using the additional loops required takes more processing power than implementing a simple bubble sort.
But for large lists, the difference is about 66% less time taken with insertion sort (depending on the type of insertion sort used) than with a selection.

For the SDD syllabus - you DONT need to know the different types of insertion sorts. However, you need to associate Insertion with SHUFFLING.

Have a look at this program: Generate a list of data, and slow down the sorting process so you can actually see what is happening: (its only a 25kb zip file - so check it out! - its well worth it.)
http://www.fosweb.com/files/sort.zip
 

Ragerunner

Your friendly HSC guide
Joined
Apr 12, 2003
Messages
5,472
Location
UNSW
Gender
Male
HSC
2003
Wow. Thats a cool looking program.

Too bad I don't understand how to use it :p

For the exam im just going to memorise the algorithm and slam it down when they ask..
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
Generate data with about 25 items with width 20. Drag the delay bar to slow down the sorting.

Just a graphical demo to show what each does.

Originally posted by Ragerunner
For the exam im just going to memorise the algorithm and slam it down when they ask..
Yeah - whilst it helped me writing it, and it may help you looking at it - it probably won't benefit you that much more than just learning the algorithms.
 

Lundy

Banned
Joined
Sep 2, 2003
Messages
2,512
Location
pepperland
Gender
Female
HSC
2003
algorithms are my software achilles heel, unfortunately. I learn things in writing better than I do from trying to decipher an algorithm
 

Comedy_Al

Member
Joined
Aug 28, 2003
Messages
109
Location
Newcastle
Algorithms i find are the easiest, but that could be because i'm a maths person (3u) and suck at english (like REALLY suck):)
 

Ragerunner

Your friendly HSC guide
Joined
Apr 12, 2003
Messages
5,472
Location
UNSW
Gender
Male
HSC
2003
Algorithms don't really have anything to do with being good at maths??
 

Comedy_Al

Member
Joined
Aug 28, 2003
Messages
109
Location
Newcastle
I guess... but thats in comparison to the free response questions. eing able to do a quick desk check in your head ( a head check?) helps. And maths does have a lot to do with being able to do a nice elegant bit of code- so i guess being able to do 3d trig might not be usefull, but other things are.
 

Lundy

Banned
Joined
Sep 2, 2003
Messages
2,512
Location
pepperland
Gender
Female
HSC
2003
so is it a coincidence that I'm also bad at maths, or is there a connection there?
 

:: ck ::

Actuarial Boy
Joined
Jan 1, 2003
Messages
2,414
Gender
Male
HSC
2004
i think algorithms has to do with logical thinking
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
Originally posted by :: ryan.cck ::
i think algorithms has to do with logical thinking
That's true, it's the size of the algorithm that scares people, people never think logically, as in looking at bits section by section,
 

JRasnier

Member
Joined
Apr 24, 2003
Messages
416
Winston, when you said 'bits section by section' I was thinking u accidentally said bits as in binary, shows how stuffed in the head i am lolz....:D
 

fatmuscle

Active Member
Joined
Jul 6, 2002
Messages
3,707
Location
Hornsby
Gender
Male
HSC
2001
does anyone want simple code for the different sorting methods?

(java language)

I have them all in lecture notes from uni
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
fatmuscle:

Do you ever do the 'maths' of sorting equations at uni? (ie: The maths of best and worst case sorts/ Bubble,Selection,Insertion being O(n^2) equations vs Quick,Merge,Heap being O(nlog(n))?)

Do you get into cool sorting algorithms? (ie: Things like Breadth/Depth First Sorts?)
 

fatmuscle

Active Member
Joined
Jul 6, 2002
Messages
3,707
Location
Hornsby
Gender
Male
HSC
2001
yeah, we touched on it.

cool sorting algorithms?

like shell short? and quick sort?
 

fatmuscle

Active Member
Joined
Jul 6, 2002
Messages
3,707
Location
Hornsby
Gender
Male
HSC
2001
yeah, we touched on it.

cool sorting algorithms?

like shell short? and quick sort?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top