selection-sort

Two versions of selection sort

风流意气都作罢 提交于 2019-12-11 18:50:59
问题 Recently i have been looking into sorting algorithms, and like many intro to algorithms books the one I have begun to read starts off with a selection sort implementation. the code went as follows... Implementation A //a is an array of ints. int n = a.length; for(int i = 0; i < n; i ++){ int min =0; for( int x = i+1; x <n;x++){ if(a[x].compareTo(a[i])<0){ Comparable tmp = a[i]; a[i] = a[x]; a[x] = tmp; } } } After analyzing the code block i altered the algorithm to the following.

String Selection Sort in C++

时光总嘲笑我的痴心妄想 提交于 2019-12-11 14:57:31
问题 Need some help with string for my selection sort. Here's what I have so far. #include "stdafx.h" #include <iostream> #include <string> using namespace std; //Constant globals const int NUM_NAMES = 20; //Function protoypes void selectionSort(string [], int); void showArray(const string [] , int); int main() { string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allet, Jim", "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri", "Johnson, Jill", "Aliison, Jeff", "Weaver, Jim",

Are Selection or Insertion sort useful outside of academic environments?

寵の児 提交于 2019-12-11 10:43:09
问题 Do these sorting algorithms have any use in real world application? Or is it just a basic example of sorting algorithm with n^2 complexity? Can anyone give some example of its usage? 回答1: Insertion sort is one of the fastest sorting algorithm for sorting very small arrays. In practice, many quicksort / mergesort implementations stop when the subarrays to sort is below certain threshold, and insertion sort is then used for these small arrays. Selection sort is rarely used in practice. 回答2:

Java: Selection Sort My implementation vs. another

元气小坏坏 提交于 2019-12-11 05:27:43
问题 The following is my implementation of Selection Sort: package algorithm.selectionsort; public class SelectionSort { public static void main(String[] args) { int[] myArray = selectionSort(new int[] { 9, 9, 9, 8, 7, 73, 32, 109, 1100, 432, 321, 0 }); for (int element : myArray) { System.out.print("" + element + " "); } } public static int[] selectionSort(int[] a) { int min; for (int i = 0; i < a.length - 1; i++) { min = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j;

Java Selection Sort

孤街浪徒 提交于 2019-12-11 04:23:14
问题 I am having issues getting my sort to check every index. It skips the 3rd indices for j as in it goes i[0] , j[2] , to i[0] , j[4] I don't know why it is doing that?. Also, I am having trouble with my numbers actually be swapped. Does anybody know where my error is? static void selectionSort(int[] arr) { final long startTime = System.nanoTime(); // starts timer System.out.println("Selection Sort"); //************** Code For Sorting *****************// //***************************************

Selection sort growing ordered ranges from both ends

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 02:25:51
问题 I have written a modified version of selection sort where I consider both a minimum and maximum of an array and place them at the two ends The algorithm works like this 1. Find the minimum and the maximum value in the list. 2. Swap the minimum value with the value in the first position. 3. Swap the maximum value with the value in the last position. 4. Repeat the steps above for the remainder of the list (starting at the second position and ending at the second to last position and narrowing

Selection Sort in Java produces incorrect results

泪湿孤枕 提交于 2019-12-10 12:32:40
问题 I am new to Java and I am trying to write a selection sort program. Below is my code : public class SelectionSort { public static int a[] = {6, 4, 9, 3, 1, 7}; public static void main(String[] args) { int min, i, j; for(i = 0; i < a.length - 1; i++) { min = i ; for(j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } if (min != i) { int temp = a[i]; a[i] = a[min]; a[min] = temp; } } } for (i = 0; i < a.length; i++) { System.out.println("a : " + a[i]); } } } My input array is 6,4,9

How do I implement SelectionSort and InsertionSort on a linked list in Python?

我们两清 提交于 2019-12-08 08:04:31
问题 I've implemented a Linked List class and I have a selectionSort and insertionSort function created that works on regular lists. I need to get selectionSort and insertionSort to work on linked lists, but I'm not sure where to even start, if I'm being honest. Here's my linked list class: class Node: def __init__(self, initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext

Why is my n log(n) heapsort slower than my n^2 selection sort

蹲街弑〆低调 提交于 2019-12-03 15:38:27
I have implemented two algorithms for sorting elements from highest to lowest. The first one, takes quadratic time on the real RAM model and the second an O(n log(n)) time. The second one uses priority queues to get the reduction. Here are the timings, which are the output of the above program. the first column is the size of the random array of integers the second column is the time in seconds for the O(n^2) technique the third column is the time in seconds for the O(n log(n)) technique 9600 1.92663 7.58865 9800 1.93705 7.67376 10000 2.08647 8.19094 In spite of this great difference in

Efficiency of Bubble vs. Selection Sort

笑着哭i 提交于 2019-12-02 04:33:57
问题 I understand that the big O values for Bubble Sort and Selection Sort are the same, (n)^2, but when I try to run both with an array of size 1000, the Bubble Sort takes 962037 swaps to sort the array, while Selection Sort only takes 988 swaps to sort the array. Why are these different? 回答1: Because the complexity refers to the number of comparisons, not the number of swaps. Both need O(n^2) comparisons, but selection sort needs only n-1 swaps in the worstcase (O(n)), whereas bubblesort might