0

I have made an array in python that generates 20 random numbers 50-100. I want to know how to sort the numbers in my array. I am in 8th grade and this is for my computing class. Can this even be done? My teacher mentioned some kind of sorting algorithm, but told me not to use sort(). I am using python. Thanks this is what i have so far

from random import*
array = range(20)
for i in range(20):
    array[i] = randint(50, 100)
print array     
4
  • yes it is possible; look at "insertion sort" or "bubble sort". They seem pretty easy. Commented Mar 26, 2012 at 18:42
  • en.wikipedia.org/wiki/Bubble_sort Commented Mar 26, 2012 at 18:42
  • 2
    There is lots of information on sorting algorithms. Bubble Sort is one of the most simple sorting algorithms, and one I would recommend starting with. Commented Mar 26, 2012 at 18:45
  • 3
    If you just want to get your homework done, googling for "python sorting" should be easy enough. If you really want to learn something from this, do NOT follow any links posted, but try to think about the problem on your own. Make 20-30 paper cards with numbers and try to sort them on your desk first. Make notes on how you compare and move cards. Try to implement this logic in python. Get back to us when you've got any specific questions. Commented Mar 26, 2012 at 19:00

4 Answers 4

3

Since it's an homework I won't give you the solution, but a starting point http://en.wikipedia.org/wiki/Sorting_algorithm where you can find an introduction to sorting algorithms! Just choose one and try to implement it! Then if you do something wrong you can ask for help ;)

Sign up to request clarification or add additional context in comments.

Comments

2

Everyone has posted excellent answers. I want to point you to this page, because it has a set of good animations for various different sorting algorithms.

Good luck!

Comments

0

For educational purposes, this should be a good place to start: http://en.wikipedia.org/wiki/Bubble_sort It's not a very good sorting algorithm, but once you understand the principles you can move on to more advanced sorting algorithms (which you will probably learn in class eventually).

Essentially, you compare every element to the next one, and swap them if they are out of order. Continue doing this until your array is sorted. Good luck!

Comments

0

This can be done and there are a number of ways to do it, you need to implement your own sorting algorithm, examples of which are very easy to come accross.

I will point you towards a python implementation of one of these algorithms with excellent explanation but i strongly suggest you read up on this sort of stuff as it is the foundation of computer science imho.

go here for a python quicksort program.. but please do the work around it.

Also i have to reccommend Introduction to Algorithms by thomas h cormen which is an awesome book and took me through a lot of stuff, and I still look at it to this day. A word of warning on this book.. it is not a light read.

If you find all this a bit heavy I found a youtube clip which goes through bubble sort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.