# Source CodeSorting arrays first min and max and then in a ordered list from user input.

#### voila

##### Newbie
Full Member
Hey, i am learning c++ at the moment, i have quite a bit of testing projects.
So i made a array first min and max sorting and then in a ordered list.
Maybe if someone could tell me if i could do something better, and why i should do it that way i would appreciate it...
code:

*Notelease use code tags*

C#:
``````#include <iostream>
#include <string>
int MAX,MIN = 0;
using namespace std;
int main()
{
int Sort[10];
int pancakes[10];
int a, b;

for(int x=0; x<=9; x++) // for loop to ask 10 questions ask for name and how many pancakes they ate,
{
cout << "Tell me the name of person " << x <<endl;
cout<<"how much pancakes did  " << username[x] << " eat\n" << endl;
cin >> pancakes[x];

if(pancakes[MAX] < pancakes[x])MAX = x;
if(pancakes[MIN] > pancakes[x])MIN = x;// checking the max and min values on eaten pancakes
}

cout << "The person that ate most pancakes is Person " << username[MAX] << " that ate: " << pancakes[MAX] << " pancakes\n" << "The person that ate least pancakes is Person " << username[MIN] << " that ate: " << pancakes[MIN] << " pancakes\n" << "Press any key to get the ordered list from least to most" << endl;//printing who ate most and least
system("pause");

for(b=0;b<=9;b++)//set array sort to a value so we can use it later
{
Sort[b]=b;
}

for(b=0; b<=8; b++)
{
for(a=b+1;a<=9;a++)
{
int temp;
if(pancakes[Sort[b]] > pancakes[Sort[a]])// if  variable b value is greater than var a

{
temp = Sort[b];//assign temp to value of whatever b is
Sort[b] = Sort[a];// taking the pancake a and puting it into pancake b
Sort[a] = temp;// putting the value of num a as the temp. so this is where the loop becomes usefull.
}
}
}
cout << "ordered list " << endl;
for(b=0;b<=9;b++)
{
cout << "Name : "<< username[Sort[b]] << " ate "<< pancakes[Sort[b]] << " pancakes" << endl;

}
system("pause");
}``````

Its working no errors but just want to see if i am heading in the right path! if my comments are right etc.

#### Liduen

##### Hacker
Dank Tier VIP
Well, I'm still not good at programming, but I would say this code is well written.
But I could not really figure out how these two for{} loops work. But this is my problem.

This example really helps when calculating which one of the enemies is closest to you and at who to aim at, when the first enemy died.
I'll need that for my aimbot ^^

#### voila

##### Newbie
Full Member
Well, I'm still not good at programming, but I would say this code is well written.
But I could not really figure out how these two for{} loops work. But this is my problem.

This example really helps when calculating which one of the enemies is closest to you and at who to aim at, when the first enemy died.
I'll need that for my aimbot ^^
Thanks, it took time for me to understand how the for loop works but i would say once you sit and maybe try 1 hour to test whats working and whats not it will make sence.
caues i was trying first to do an if statement 10 times, which isnt good and not really flexible.
So i remembered the index ordering (if its called that) its some ordering method done by a guy long time ago and i managed to write it after some hour.
Might sound redicoulous that im trying some hour on some number sorterer but i learn stuff the best that way!
ty for ur feedback

#### c5

##### Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
Bubble sort isn't the fastest method afaik.

#### Fleep

##### Founder
Meme Tier VIP
Bubble sort isn't the fastest method afaik.
Yh I would recommend either the quick sort or heap sort for speed.

Bubble is pretty much as slow as it gets.

Fleep

#### TastyHorror

##### Coder
Dank Tier Donator
Nobleman
People actually use bubble sort? What is this madness.

#### voila

##### Newbie
Full Member
Yh I would recommend either the quick sort or heap sort for speed.

Bubble is pretty much as slow as it gets.

Fleep
Oh so this is "bubble sort"
thanks for the feedback, i thought this was "index" sorting.
As i remember watching some tutorial LONG time ago on java, took the method from there.
note that i am learning c++ and i created this without watching any tutorial..
Can you tell me the logic behind quick sort? like i dont want any code or something..
But if i google "quick sort" i will get the code, so thanks if u wanna tell me.

#### c5

##### Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
People actually use bubble sort? What is this madness.
It's the easiest way to sort small amount of items in a simple order?

#### xPaay

##### Newbie
There's no best sorting algorithm. Every algorithm is better then others on some cases. This site has some good animations with the most used sorting algorithm.

https://www.sorting-algorithms.com/

Btw I've got a schoolwork where I needed to get the time to sort a 1 million+ vector in three different cases and with at least 4 sorting algorithm. In my case bubblesort was the worse on most cases, but in one specific test, quicksort was slower then bubble.

#### Syperus

##### RTFM
Meme Tier VIP
Dank Tier Donator
I would also change system("pause") to std::cin.get() so it can be used on multiple OS's. Irregardless of what method you use it was a good learning exercise I'm sure.

Attention! Before you post:

99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

• Fill out the form correctly
• Tell us the game name & coding language