First, spend some time understanding the sorting algorithms given here. Then, wr
ID: 3639035 • Letter: F
Question
First, spend some time understanding the sorting algorithms given here. Then, write a Python program
that asks the user for two things:
1. the name of an input file, which you can assume consists of integers, one per line
2. the name of an output file to store the sorted numbers in
Your program will read in the numbers from the given input file, sort these numbers in ascending order using selection sort, and write these numbers in sorted order (smallest to largest) to the given output file, one number per line.
One important caveat: your version of selection sort will differ from the one in class by virtue of selecting the largest element each time through list (rather than the smallest) and moving it to its correct position on the right (rather than moving the smallest number into its correct position on the left).
Recall that the pseudocode for selection sort studied in class (and in the handout) looks like:
i = 0
while (i < N-1)
m = i
j = i+1
while (j < N) # X[m] will be the smallest element of the list
if (X[j] < X[m])
m = j
j = j+1
swap(i,m) # Exchange smallest element with leftmost unsorted location
i = i+1
Your code should instead look for the largest element each time through the inner loop and move it to the rightmost unsorted location. If you perform the algorithmic analysis, you will see that the two algorithms are equivalent (in terms of expected run time), even though the programs will look somewhat different.
All the tools you need for this assignment have been given to you. Read through the textbook’s selection sort carefully. Both selection sort and insertion sort use a “sorted” and “unsorted” portion of the list. In the example given in class, we have selected the beginning of the list to be sorted (the smallest elements first), while the book’s selection sort starts with the end of the list as the sorted portion (the largest elements are sorted first). Your program should conform to the book’s approach, although the algorithms are functionally equivalent.
Explanation / Answer
Check the following sites, I hope that it will very useful sites for you... http://docs.python.org/faq/programming.html http://www.devx.com/dbzone/Article/22093/1954
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.