Cory Posted May 21, 2009 Report Share Posted May 21, 2009 I need to sort some lists not greater than a thousand or so and in this example I will be using arrays but I don't think that's important. But for the sake of this thread think arrays. I could use a bubble sort but it is terribly inefficient so I was hoping to find a better method. However most of the algorithms I see online that are efficient become complex really quick. Also it seems like they mostly are limited to one array. Can any one recommend a simple method I could use in MEP? I had my own idea before my research but can not find it anywhere. It's similar to an Insertion Sort but might be more efficient. The analogy of an Insertion Sort how a human might sort a deck of cards. The person starts with one deck and takes the top card in hand. They then pull another card and if it is greater than the card in hand it goes to the back else front. Subsequent cards get compared to every card until it's place is found where the one in front is smaller and the one behind is larger and it's inserted there. I like it because it's conceptually simple and way more efficient than a bubble sort. But there is one aspect I don't like. In order to do this in an array one takes the next value and then starts from the highest element in the array and as it moves down thru the elements moves each one up an element number until it's place is found so that can be a lot of operations. Now my idea was to have two arrays. In the first one we take one of the value and compare it to ever element in the list that is less than or equal to it. This value is it's element in the second array and I copy it there. Now I do this for every element in the first array. The untidy bit is if I have duplicate items in the first list. To address this I reckon before I copy to the new array I could first see if that slot is taken. If it is decrement the element number and repeat until an empty slot is found. My biggest wonderment here is if my method is really any more efficient. Reflexively it could be but after some consideration of the counting I'm wondering if it really is. Do you think my method is any better? After working on this problem awhile I think for the job at hand I will be using an insert sort but the main reason for that is that since it doesn't need to know the entire set in advance I can use it on the fly. So if I am grabbing variables from some source I can insert sort them as they are gathered instead of having to create an unsorted list first. Also I can embed some comparisons and other tests for things like duplicates in line and for that it would actually be more efficient because the list is already sorted at any point in time so I can quit when a hit is registered. Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.