Cod sursa(job #3039095)

Utilizator CapotaLucasLucasCapota CapotaLucas Data 28 martie 2023 10:20:02
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
// Insertion Sort - Complexitate O(n^2)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

 
ifstream in ("algsort.in");

ofstream out ("algsort.out");

 
const int dim = 500001;

 
int v[dim];

 
int
main () 
{
  
int n, i, j, elem;
  
 
in >> n;
  
for (i = 1; i <= n; i++)
    {
      
in >> v[i];
    
}
  
 
    // insertion sort
    for (i = 2; i <= n; i++)
    {
      
elem = v[i];
      
j = i - 1;
      
while (j >= 1 && v[j] > elem)
	{
	  
v[j + 1] = v[j];
	  
j--;
	
}
      
v[j + 1] = elem;
    
}
  
 
    // afisare vector v
    for (i = 1; i <= n; i++)
    {
      
out << v[i] << " ";
    
}
  
 
return 0;

}