Ok so straight to the point, I'm building a simple bubble sort program and i am implementing a user defined array size. So, the code shown below compiles with no errors however, when i run the program it crashes when i go to enter the second element. Any help/tips would be apprecited. ( This is only the early stage of the code)
#include<iostream>
using namespace std;
int main()
{
int usersize;
cout << "Welcome to this sorting engine. Please enter in your array size" << endl;
cin >> usersize;
int* Numbers = new int[usersize];
cout << " Please enter in elements" << endl;
scanf("%d"), Numbers;
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
std::istream& operator >> ( std::istream& ins, char c )
{
ins >> std::ws;
if (ins.peek() == c) ins.get();
return ins;
}
int main()
{
std::vector <int> numbers;
{
std::cout << "Enter a list of whole numbers:\n";
// Get user input as a string
std::string s;
getline( std::cin, s );
// Convert the string into a list of integers
std::istringstream ss( s );
int x;
while (ss >> x >> ',')
numbers.push_back( x );
// Complain if the user entered something that is not an integer
if (!ss.eof())
{
ss.clear();
std::cerr << std::string( (int)ss.tellg(), ' ' ) << "^ not a whole number\n";
return 1;
}
}
// now you can sort the numbers
...
Enter a list of whole numbers:
1,2, 3 , 4 5 ,6 7
Enter a list of whole numbers:
1 2 3 -4 five 6,7
^ not a whole number
Enter a list of whole numbers:
1, 2, 3.14159, 4
^ not a whole number
I would argue that its about 5 extra lines to write shellsort which is almost as fast as the best sorts out there (its O(n^(x+1)/x) which is slower but generally comparable to NlgN up to fairly large values) yet the code is still very simple, no recursion or anything. Beats me why the profs won't teach it. Selection and insertion are pretty much as bad as bubble. I can't recall but the final pass of shell is insertion or selection, effectively. Its actually still in use in a number of high performance applications for small arrays.
Selection sort is the most natural of all sorting algorithms — it is how people tend to sort things normally. Insertion sort is almost as natural as selection sort, and also in common use by people knowing nothing about CS. Both are very, very easy to understand and implement.
The reason insertion sort remains prominent is because for lists small enough to fit in your L1 cache it is the fastest sorting algorithm bar none. That is why so many algorithms switch to insertion sort once the divide and conquer stage gets to fewer than some number of elements (usually between 30 and 60, depending on your author and any potential tuning).
Shell sort adds an odd complexity (by interleaving) that is not really easy to follow or justify in a beginner’s mind. Both quicksort and mergesort are easier to understand: split the list, repeat on each of the smaller lists, then join the lists back together. They are also stupendously easy to implement, even badly, and avoid having to handle the multiple indexing issues at the same time that you do in bubble and shell sorts.