Efficient way to sort large set of numbers



I have to sort a set of 100000 integers as a part of a programming Q. The time limit is pretty restrictive, so I have to use the most time-efficient approach possible.

My current code -

#include<cstdio>
#include<algorithm>
using namespace std;

int main() {
    int n,d[100000],i;
    for(i=0;i<n;++i) {
                     scanf("%d",&d[i]);
    }
    sort(d,d+n);
    ....
}

Would this approach be more efiicient?

int main() {
    int n,d[100000],i;
    for(i=0;i<n;++i) {
                     scanf("%d",&d[i]);
                     sort(d,d+i+1);
    }
    ....
}

What is the most efficient way to sort a large dataset?

Note - Not homework...


Related to : Efficient way to sort large set of numbers
Efficient way to multiply large matrices
Programming Languages
I am working with some large (over 1 million x 1 million), sparse matrices. I need to multiply 7 of them together, and I am currently using Pythons scipy.sparse modules with the lil_matrix sparse class (before multiplication, I need to slice them, etc). When doing the multiplications, I can get 6 of them to multiply (it appears in any order), yet the 7th fails with the following error message:
PHP Code:
File "/usr/local/packages/scipy/0.7.1/lib/python2.4/site-packages/scipy/spars
e/base.py", line 288, in __mul__
   &nbs
Sort a user input list of numbers using python bubble sort
Programming Languages

I've just started to learn python and I've decided to try and do a bubble sort. I've used the code below which works ok if the numbers to be sorted are 0 to 9. After that, it doesn't sort them correctly. I think, in my limited knowledge that this is because it is a 'list'.

I would like the user to be able to input the numbers but for the program to sort them regardless of the length of the number. Any help would be appreciated.

def bubble_sort(items):
    changes=0

    for i in range(len(items)):
        for j in range(len(items)-1-i):#-i = optimised??
            if items[j] > items[j+1]:
                items[j], items[j+1] = items[j+1], items[j]  # Swap
                changes=changes+1

    print(items)
    print("Number of passes =",i)
    print("Number of swaps =",changes)

print("Welcome to a Bubble Sort Algorithm in Python!")

while True:

    print("Enter as many numbers as you want.
 You can choose between 0 and 9.
Leave a space between each one")
    numbers=input()
    items=numbers.split()

shift even numbers in an array to left and odd numbers to right and sort even numbers in ascending order and odd numbers in descending order
Programming Languages

The question is how do I do this using bubble sort and in blue j using 2 loops and an if check having 3 conditions only?


Most efficient way to find smallest of 3 numbers Java?
Programming Languages

I have an algorithm written in Java that I would like to make more efficient. A part that I think could be made more efficient is finding the smallest of 3 numbers. Currently I'm using the Math.min method as below:

double smallest = Math.min(a, Math.min(b, c));

How efficient is this? Would it be more efficient to replace with if statements like below:

double smallest;
if(a<b && a<c){
    smallest = a;
}else if(b<c && b<a){
    smallest = b;
}else{
    smallest = c;
}

Or if any other way is more efficient

I'm wondering if it is worth changing what I'm currently using?

Any speed increase would be greatly helpful


Looking for the most efficient way to count sort using threads and locks
Programming Languages

I am looking for the most efficient (fastest) way to count how many times each letter appears in a very large text file. The basic idea is to use multi threading, so all 4 cores can participate, while keeping in mind that the locks that are used for synching takes time and lower efficiency. So, how would you approach the problem? Remember, every microsecond counts!


Edit: I got this as an interview question, and like others commented, I thought it should be done using one thread but it was not accepted. I suggested to split the file in four and let each thread count one part, but the interviewer insisted on a "true multithreaded solution".


Efficient way to sort large set of numbers
Programming Languages

I have to sort a set of 100000 integers as a part of a programming Q. The time limit is pretty restrictive, so I have to use the most time-efficient approach possible.

My current code -

#include<cstdio>
#include<algorithm>
using namespace std;

int main() {
    int n,d[100000],i;
    for(i=0;i<n;++i) {
                     scanf("%d",&d[i]);
    }
    sort(d,d+n);
    ....
}

Would this approach be more efiicient?

int main() {
    int n,d[100000],i;
    for(i=0;i<n;++i) {
                     scanf("%d",&d[i]);
                     sort(d,d+i+1);
    }
    ....
}

What is the most efficient way to sort a large dataset?

Note - Not homework...



Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .