Learn how to sort.

Revision en1, by visitorG, 2026-03-23 07:44:12

Hey,

In this blog, I will tell you, how to sort a vector without sort(a.begin(), a.end()) because I don't know how it works. So I just randomly made this( I don't really know If it ever existed, so just listen to what I say because I spent 30 minutes just figuring out the idea..)

void sort() {
  int n;
  cin >> n;
  vector<int>a(n);
  for(int i=0;i<n;i++) {
    cin >> a[i];
  }
  while(!(is_sorted(a.begin(), a.end()))) {
    for(int i=1;i<n;i++) {
      if(a[i]<a[i-1]) {
        swap(a[i], a[i-1]);
      }
    }
  }
  for(auto e:a) {
    cout << e << " ";
  }
  cout << endl;
}

I wont add comments, because that will make me look like a cheater. So, Listen to this:

"What I did in here was I first checked if the list was not sorted and then a simple loop. The main thing comes now, I checked whether a[i] is less than a[i-1] and if it was, I would just simply swap them(that was kinda simple tbh). Afterwards I just "couted" them."

I think that you might have understand what I just told you about "Sorting".

Tags sorting, method, math, sort

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English visitorG 2026-04-03 16:35:10 97
en1 English visitorG 2026-03-23 07:44:12 1084 Initial revision (published)