I need a algorithm to find the smallest number in an array. C++
If you do not have a sorted list, you will probably end up using a bubble sort.
I need it to make a sorted list out of an unsorted list.
so far I have something like this.. for (x=0; x<10; ++x){ if (UnList[x] < s){ s = UnList[x]; } else if (++UnList[x] < s){ s = UnList[x]; } cout << s << "," ; } I not sure if its really working or not..
hahah no.. its not lol
Presumably you have initiated 's' to some number greater than you expect to find in your array?
no, cause I not expecting what the user is going to input.
int s = 1000; int x; for(x=0; x<10; x++){ if (unList[x] < s) { s = unList[x]; } } cout << s << endl;
So your user is going to put in a number and you are going to get the array from?? and spit out the number smaller than the user input?
Perhaps you could start by stating what it is you are trying to accomplish.
#include <iostream> #include <string> using namespace std; int main() { int UnList[10]; int SList[10]; int i,j,n,x,s; int nVal; // Ask user to insert the values for the Unsorted List. cout << "Please insert 10 integers numbers:" << endl; for(i=0; i< 10; ++i){ cout << "[" << i <<"]" << "= "; cin >> nVal; cout << endl; UnList[i]={nVal}; }// end of loop for storing the values. //Display the Unsorted List cout << "Unsorted List: "; for(j=0; j<9; ++j){ cout << UnList[j] << ","; } cout << endl; cout << "Sorting ..."; cout << endl; //Selection Sort Algorithm for (n = 0; n <9; ++n){ for (x=0; x<10; ++x){ if (UnList[x] < s){ s = UnList[x]; } else if (++UnList[x] < s){ s = UnList[x]; } SList[n] = {s}; }// end of the Linear Search method... } //end of the Sorting Method... //Display Sorted List cout << "Sorted List: "; for(i = 0; i<9; ++i){ cout << SList[i] << " "; } }
that is the code. Tasks: 1) Ask the user to insert 10 integer number and store them in a unsorted array. 2) use the Sorting Method to sort the array. 3) Display the Sorted Array.
OK, I think my method is working .. but the output is not what I want..
The first thing you will want to do is assign an index to your 's', s=UnList[0]; Then when you step into your array you are just swapping.
Here is a bubble sort I wrote for a project. for(i = 1; i <= numLength; i++) { for (j=0; j < (numLength -1); j++){ if (t_array[j+1] > t_array[j]){ temp = t_array[j]; // swap elements t_array[j] = t_array[j+1]; t_array[j+1] = temp; } } }
Nice. I used it to find the smallest.. cause I think u have it set it up to find the largest. Thanks, it helped .. still Im not getting the desired output, now Im working to find out why
I had to change the last for loop to print out all the values: http://ideone.com/alTSNf
The code does not presently preserve the unsorted list, but that wouldn't take much to fix if you wanted it saved.
thanks!!, Im not gonna see it yet.. I'll try to find it out by myself then either I give up or not I check ur code :)
Good on ya, put in the work and feel better about the win. :D
Join our real-time social learning platform and learn together with your friends!