Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
insertion_sort
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <iostream> #include <cstdlib> using std::cout; //Template, sorts in nondecreasing order for anything where ">" and "<" are defined template<class T> T* insertion_sort1(T array[], int size) { int j = 1,i; T key; while(j < size) { key = array[j]; i = j - 1; while(i >= 0 && array[i] > key) { array[i+1] = array[i]; array[i] = key; i--; } j++; } return array; } // Sorts in nonincreasing order for anything where ">" and "<" are defined template<class T> T* insertion_sort2(T array[], int size) { int j = 1,i; T key; while(j < size) { key = array[j]; i = j - 1; while(i >= 0 && array[i] < key) { array[i+1] = array[i]; array[i] = key; i--; } j++; } return array; } template<class T> void print(T array[], int size) { int i = 0; size--; cout<<"{"; while(i < size) cout<< array[i++] <<","; cout<<array[i]<<"}"; } int main(int argc, char* argv[]) { int length = argc-1; if(length == 0) { cout<<"Enter elements of the array to be sorted via the CLI thus: "; cout<<"\"./a.out 31 4 59 26 41 58\"\n"; return 0; } //int a[length]; float b[length]; int i = 0; while(i < length) { // a[i] = atoi(argv[i+1]); b[i] = atof(argv[i+1]); i++; } cout<<"Sorting array: "; print(b,length); cout<<"\n"; cout<<"(increasing order) --> "; print(insertion_sort1(b, length),length); cout<<"\n"; cout<<"(decreasing order) --> "; print(insertion_sort2(b, length),length); cout<<"\n"; return 0; }
g++
31 4 59 26 41 58
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 0.44 sec, absolute running time: 0.11 sec, cpu time: 0.06 sec, memory peak: 3 Mb, absolute service time: 0,57 sec
fork mode
|
history
|
discussion
Enter elements of the array to be sorted via the CLI thus: "./sorting 31 4 59 26 41 58"