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 7490339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:35:06+00:00 2026-05-29T15:35:06+00:00

I have a file that has 1 million numbers. I need to know how

  • 0

I have a file that has 1 million numbers. I need to know how I can sort it efficiently, so that it doesn’t stall the computer, and it prints ONLY the top 10.

#!/usr/bin/python3

#Find the 10 largest integers
#Don't store the whole list

import sys

def fOpen(fname):
        try:
                fd = open(fname,"r")
        except:
                print("Couldn't open file.")
                sys.exit(0)
        all = fd.read().splitlines()
        fd.close()
        return all

words = fOpen(sys.argv[1])

big = 0
g = len(words)
count = 10

for i in range(0,g-1):
        pos = i
        for j in range(i+1,g):
                if words[j] > words[pos]:
                        pos = j
                if pos != i:
                        words[i],words[pos] = words[pos],words[i]
                count -= 1
                if count == 0:
                        print(words[0:10])

I know that this is selection sort, I’m not sure what would be the best sort to do.

  • 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-05-29T15:35:07+00:00Added an answer on May 29, 2026 at 3:35 pm

    If you only need the top 10 values, then you’d waste a lot of time sorting every single number.

    Just go through the list of numbers and keep track of the top 10 largest values seen so far. Update the top ten as you go through the list, and print them out when you reach the end.

    This will mean you only need to make a single pass through the file (ie time complexity of theta(n))

    A simpler problem

    You can look at your problem as a generalization of finding the maximum value in a list of numbers. If you’re given {2,32,33,55,13, ...} and are asked to find the largest value, what would you do? The typical solution is to go through the list, while remembering the largest number encountered so far and comparing it with the next number.

    For simplicity, let’s assume we’re dealing with positive numbers.

    Initialize max to 0
    0 < 2, so max = 2
    2 < 32, so max = 32
    32 < 33, so max = 33
    33 < 55, so max = 55
    55 > 13, so max = 55
    ...
    return max
    

    So you see, we can find the max in a single traversal of the list, as opposed to any kind of comparison sort.

    Generalizing

    Finding the top 10 values in a list is very similar. The only difference is that we need to keep track of the top 10 instead of just the max (top 1).

    The bottom line is that you need some container that holds 10 values. As you’re iterating through your giant list of numbers, the only value you care about in your size-10-container is the minimum. That’s because this is the number that would be replaced if you’ve discovered a new number that deserves to be in the top-10-so-far.

    Anyway it turns out that the data structure best fit for finding mins quickly is a min heap. But I’m not sure if you’ve learned about heaps yet, and the overhead of using a heap for 10 elements could possibly outweigh its benefits.

    Any container that holds 10 elements and can obtain the min in a reasonable amount of time would be a good start.

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

Sidebar

Related Questions

I have a file that has around million lines. I need to go to
I have a tab-delimited file that has over 200 million lines. What's the fastest
I have a html file that has invoice details I would like to know
I have a huge file that has some lines that need to have a
I have a text file that has item numbers in it (one per line).
I have a csv file that has 3.5 million codes in it. I should
I have a test file that has many lines, each line looks something like:
I have an XML file that has a number of nodes, each of which
I have an image file that has all the character sprites that I will
I have an IDL file that has a #pragma prefix directive, but whenever I

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.