sorting method

Hello, first off I would just like to ensure that this is not for homework or anything like that because I am past the first class of cs111 where by now we should know how to do something like this already, I am just trying to make my own sorting method to have for the future so can anyone please help me on this? When it prints I get 0 so I don't see what I am doing wrong or where I am going wrong please help! Here is my code so far.

#include <iostream>
using namespace std;
int main(){
int a[4] = {0,1,2,3};
int b[4] = {4,2,1,0};
for(int i=0; i<=3; i++){ //read a[i] from left to right
for(int j=3; j>=0; j--){ //read b[j] from right to left
while(!b[j]==a[i]) // comparion
if(b[3]==a[3] && b[2]==a[2] && b[1]==a[1] && b[0]==a[0]); //comparison test
int temp=a[i];
a[i]=b[j];
b[j]=a[i];
cout << b[j];
}
cout << endl;
}
system("pause");
return 0;
}
If your assignments do not require you to create your own sorting method, why are you trying to? Just use std::sort.
Cause I want it for future references if I ever need to do a program that requires sorting so I can always have it on me whats std::sort a function?
Here is one done before by @cire

http://www.cplusplus.com/forum/beginner/92495/#msg497709

Converted it to use arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
typedef unsigned int uint;

void Sort(int array[], int size)//Bubble sort function 
{
  bool Sort;
  
  do
  {
    Sort = false ;
    
    for(uint i = 1; i < size; ++i)
      if (array[i] < array[i-1])
      {
	int temp = array[i];
	array[i] = array[i-1];
	array[i-1] = temp;
	Sort = true;
      }    
    
  } while ( didSort );
      
}



Sort function is found in the algorithm library
http://www.cplusplus.com/reference/algorithm/sort/
Last edited on
C++'s std::sort works on arrays too, not just STL containers.
Last edited on
Hey lb thanks, but anyway you can help improve my code? and smac89 thanks but i don't want some other code that's already out there would rather much appreciate it if you can help me improve my code?
You will have to explain the logic you are trying to apply to that sorting function of yours. It is taking over 8 seconds and still counting to sort an array containing 4 values. The reason for this is the while loop in the body of the second for-loop. What is this supposed to be doing?
Topic archived. No new replies allowed.