Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:18:20+00:00 2026-06-03T01:18:20+00:00

What i am trying to do is to prompt the user for an sort

  • 0

What i am trying to do is to prompt the user for an sort function type, sort patter, array size, size of array increment and number of test. Then i want it to save it. However, there are couple problems with this program.

  1. Somehow when i choose the random pattern it gives me some weird answer like:

    1543 0.002

    600 0.020

    1400 0.08

Its not really in an order. I think that something wrong is with the for loop.

def rand_array(n):
''' returns sorted array of integers of size n'''
    R=[randint(1, 1000*n) for i in xrange(n)]
    return R

def sorted_array(n):
    ''' returns a sorted array of n integers'''
    return [i for i in xrange(1,n+1)]

def rev_array(n):
    '''returns an array of n integers in reverse order'''
    R= [i for i in reversed(xrange(1,n+1))]
    return R

 def sort_timehelp(x,f):
    ''' This times the quick sort algorithm as it must take 3 variables'''
    high=len(x)
    low=0
    t0=clock()
    f(x,low,high)
    t1=clock()
    dt=t1-t0
    return dt

def main():
    myinfo()
    info()
    while True:
        print '==================== to quit enter Control-c=================='
        sortfunction=input("Choose a sort function: ")
        s=input("Choose a pattern: ")
        n=input("Array Size: ")
        increment=input("Increment size: ")
        y=input("Number of tests: ")

        if s == 1:
            x=rand_array(n)
        elif s ==2:
            x= sorted_array(n)
        elif s==3:
            x=rev_array(n)
        if sortfunction==1:
            i=0
            output="algorith: quick sort \n input data: %s" %s
            print output
            while i<y:
                i=i+1
                ff=0.0
                array=x[increment-1:n:increment]
                for my in array:
                    ff+=sort_timehelp(x,quick_sort)
                    output="%d\t %f" %(my, ff)
                    print output

          saving=input("You want to save data ? type 0 to continue or 1 to save " )

          if saving == 0:
              continue
          if saving == 1:
              ask=raw_input("Type the name file: ")
              fileout=open(ask+".csv","w")
              fileout.write(output)
              fileout.close()

Second problem is that when i am trying to save the data it only saves the last data, but i want to save everything.

I would appreciate any help.

Edit:
timing function takes and array and a sorting algorithm
i want to save the numbers by increments and corresponding timing to it. (thats where my for loop)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:18:22+00:00Added an answer on June 3, 2026 at 1:18 am

    There are a lot of issues. Let’s go through them…

    def rand_array(n):
    ''' returns sorted array of integers of size n'''
        R=[randint(1, 1000*n) for i in xrange(n)]
        return R
    

    This doesn’t return a sorted array of random numbers. It returns a list of random integers chosen from successively larger domains. You probably want:

    def rand_array(n):
    ''' returns sorted array of integers of size n'''
        return sorted([randint(1, 1000) for i in xrange(n)])
    

    def sorted_array(n):
        ''' returns a sorted array of n integers'''
        return [i for i in xrange(1,n+1)]
    

    This should simply be:

    def sorted_array(n):
        ''' returns a sorted array of n integers'''
        return range(1, n + 1)
    

    def rev_array(n):
        '''returns an array of n integers in reverse order'''
        R= [i for i in reversed(xrange(1,n+1))]
        return R
    

    is simply:

    def rev_array(n):
        '''returns an array of n integers in reverse order'''
        return reversed(sorted_array(n))
    

                i=0
                output="algorith: quick sort \n input data: %s" %s
                print output
                while i<y:
                    i=i+1
                    ff=0.0
                    array=x[increment-1:n:increment]
                    for my in array:
                        ff+=sort_timehelp(x,quick_sort)
                        output="%d\t %f" %(my, ff)
                        print output
    

    So you’re sorting as many times (in the inner loop) as you have array elements? Not sure why. Anyway, the business with i should simply be done with a for loop:

                print "algorith: quick sort \n input data: %s" %s
                for i in range(y):
                    ff = 0.0
                    array = x[increment-1:n:increment]
                    for my in array:
                        ff += sort_timehelp(x, quick_sort)
                        output = "%d\t %f" %(my, ff)
                        print output
    

              saving=input("You want to save data ? type 0 to continue or 1 to save " )
    
              if saving == 0:
                  continue
              if saving == 1:
                  ask=raw_input("Type the name file: ")
                  fileout=open(ask+".csv","w")
                  fileout.write(output)
                  fileout.close()
    

    The if saving==0 clause can be removed; any value of saving other than 1 will skip saving.

    As Scott pointed out, you want "a" instead of "w" in open. Another thing you could do is move the open and close out of the loop. You might also want to use the built-in Python csv module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to prompt the user for a weight smaller than 126. Then
I am trying to have a loop continue to prompt the user for an
I'm trying to create a program where the size of array index and its
I am trying to prompt the user to enter a string of text. Is
I'm trying to prompt the user (once, I record it) to upgrade the iphone
I want to prompt the user to enter 2 numbers into a list of
I'm trying to prompt the user to enter a filename/path at the console and
I'm trying to write a script that will prompt a user for an answer
I'm trying to modify this Mercurial extension to prompt the user to add a
I want to prompt the user to enter a database name and warn him

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.