Search results
Results from the WOW.Com Content Network
Insertion sort : It is opposite to Selection sort where it picks first element from unsorted sub-array and compare it with sorted sub-array and insert the smallest element where found and shift all the sorted elements from its right to the first unsorted element.
why is selection sort unstable? let a be an array of integers such that a = [0, 0, 2, 3, 4]. if a is to be sorted using an unstable implementation of selection sort; first two indexes of a (a[0] = 0 and a[1] = 0) will get swapped.
However, insertion sort or selection sort are both typically faster for small arrays (i.e. fewer than 10-20 elements). A useful optimization in practice for the recursive algorithms is to switch to insertion sort or selection sort for "small enough" sublists. And, Wikipedia on bubble sort (emphasis added):
Selection sort is generally an unstable sorting algorithm. To understand this, let's consider an example: array = [3a, 7, 5, 3b, 2, 9] where 3a = 3b. After 1st iteration array = [2, 7, 5, 3b, 3a, 9] Now, you can see that the order of 3a and 3b are interchanged. And you will end up with the result as array = [2, 3b, 3a, 5, 7, 9]
After all, we don't even know the type of the variable sort. So, without knowing much about the code inhibits our ability to analyze the time and space complexity of your code. That being said, your implementation looks fairly similar to a generic selection sort pseudocode, and it can be considered as a typical implementation, I guess. –
@RayToal It is an array of arrays. I had to write a list of criminals (10 rows), and for each criminal I had to have a name, crime, year (3 columns). I want to sort the list by crimes and I have to use a selection sort, but I want the name of the criminal and year to stay with the crime. –
I'm trying to code a selection sort using ArrayList. My program requires me to create an array of size 20 and populate it with random integers between 1 and 1000 (no user input or hard code). The output will require displaying the original unsorted list of integers, and displaying each pass of the sorting algorithm on a separate line.
Here is how I would rewrite your code. Of course in Python I would just use list.sort() to sort a list, but here is a selection sort in Python. We make a generator expression that returns tuples of (value, i) for a value and its index from the list.
What makes selection sort unstable is that the step of swapping a pair of elements could possibly change the relative order of another pair of elements that have equal keys. For instance, when sorting the array. 2 2' 1 since the element with the minimum key is 1, you'll have to push it to the lowest position of the array by swapping 1 with 2: 1 ...
You should never really be put in a position that you need to create your own sort algorithm. Instead, you should always try to utilize existing collection class, eg set, which will sort for you. That being said, if your goal is to better understand pointers, then as greatwolf commented, you shouldn't be sorting pointers, but what they are ...