Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to sort numbers?

Status
Not open for further replies.

Djsarkar

Member
Given sequences = [2, 3, 4, 1]

If someone asked me to write this sequence in an increasing order, I can write it very easily sequence = [1, 2, 3, 4]

My head solved it quickly. But i don't understand how it happened. If there will be a machine in place of my mind, how will it work.
 
The simple (but often slow) approach is to use a Bubble Sort.
 
It really depends on how many empty registers you have (one vs number of variables or some where in between).
Also, are you doing this in assembly or c or c++ where lists and arrays can be used.
 
It really depends on how many empty registers you have (one vs number of variables or some where in between).
Also, are you doing this in assembly or c or c++ where lists and arrays can be used.
I would like to do it in C language but my interest is in finding more generic process. For now I don't want any program. I assuring we have array and there are four number stores in it. We need to sort array in increasing order.

Now I have paper on which I wrote the number stores into array . My head can easily sort array but i do not understand how machine will sort by program.

I am not able to think what the machine will test, how many times it will test, under what conditions, when the test will be finished?
 
The Bubble Sort algorithm is very inefficient, but it is very easy to implement, and easy to explain:
1. Start at the top of the list and compare the first two numbers. If the first number is larger than the second number then reverse their positions.
2. Go down one position and compare the second number to the third number. As before, if the second number is larger than the third number then reverse their positions.
3. Continue down to the bottom of the list, comparing and reversing as necessary.
4. When you get to the bottom of the list, go back to the top and repeat steps 1 to 3. Do this repeatedly, going through the list, until you do one pass where none of the numbers need to change position. At this point the list is sorted.

It's inefficient, because if you have N numbers in the list and the smallest number is at the bottom of the list, then you'll have to go through the list N-1 times. Other algorithms are much more efficient, but more difficult to expain.
 
To speed up large sorts of say alphanumeric data, use an index array. You swap the index, not the data. it saves lots of time/

eg, P(i) =1 FOR I = 1 to n-1 for starters; P(n)=n

Loop:
Interchange = False
For i = 1 to n-1
if A$(P(i)) < A$(P(i+1) then switch(P(i), P(i+1)) ; Interchange=TRUE
Next i
If Interchange=True goto Loop
 
 
Status
Not open for further replies.

Latest threads

Back
Top