//se numeste algoritmul prin insertie
public class Main {
public static void InsertSort(int[] vec) {
int n = vec.length;
//{7, 6, 5, -4,3,2,-1,0};
//{2, 7, 5,-4,3,2,-1,0};
//{2, 5, 7,-4,3,2,-1,0};
// i:1 2 3 4 5 6 7 8
//aux = vec[i] = 6
//j= i-1 = 0
//vec[0] = 7
for(int i = 1; i < n; ++i) {
int aux = vec[i];
int j = i - 1;
while(j >= 0 && vec[ j ] > aux) {
vec[j+1] = vec[j];
j-=1;
}
vec[j+1] = aux;
}
}
public static void main
(String args
[]) {
//declaram un vector de intregi
int[] vector = {7,6,5,-4,3,2,-1,0};
//afisam pe ecran un string
System.
out.
println("vectorul original");
//afisam vectorul pe stdout
DisplayVector( vector );
InsertSort( vector );
//afisam pe ecran un string
System.
out.
println("vectorul sortat");
//afisam inca o data vectorul sortat
DisplayVector( vector );
}
public static void DisplayVector(int[] vec) {
for(int value: vec) {
System.
out.
print(value
+ " "); }
}
}
/*
VECTOR: 7 8 1 5 9
i: 0 1 2 3 4
j = i - 1
<----
for(i = 1; i < lungime_VECTOR; ++i) {
temp = vec[i]
j = i - 1;//j = 0
while(j >= 0 SI vec[j] > temp) {
vec[j+1] = vec[j]
j--;
//j = j -1
}
vec[j+1] = aux;
//vec[-1+1] = aux
//vec[0] = aux
}
*/
Ly9zZSBudW1lc3RlIGFsZ29yaXRtdWwgcHJpbiBpbnNlcnRpZQpwdWJsaWMgY2xhc3MgTWFpbiB7CgogICAgICAgcHVibGljIHN0YXRpYyB2b2lkIEluc2VydFNvcnQoaW50W10gdmVjKSB7CgogICAgICAgICBpbnQgbiA9IHZlYy5sZW5ndGg7CiAgICAgICAgIAogICAgICAgICAvL3s3LCA2LCA1LCAtNCwzLDIsLTEsMH07CiAgICAgICAgIC8vezIsIDcsIDUsLTQsMywyLC0xLDB9OwogICAgICAgICAvL3syLCA1LCA3LC00LDMsMiwtMSwwfTsKICAgICAgICAgIC8vICBpOjEgMiAzIDQgNSA2IDcgOAogICAgICAgICAgICAvL2F1eCA9IHZlY1tpXSA9IDYKICAgICAgICAgICAgLy9qPSBpLTEgPSAwCiAgICAgICAgICAgIC8vdmVjWzBdID0gNwogICAgICAgICBmb3IoaW50IGkgPSAxOyBpIDwgbjsgKytpKSB7CgogICAgICAgICAgICAgaW50IGF1eCA9IHZlY1tpXTsKCiAgICAgICAgICAgICBpbnQgaiA9IGkgLSAxOwoKICAgICAgICAgICAgIHdoaWxlKGogPj0gMCAmJiB2ZWNbIGogXSA+IGF1eCkgewoKICAgICAgICAgICAgICAgdmVjW2orMV0gPSB2ZWNbal07CgogICAgICAgICAgICAgICBqLT0xOwogICAgICAgICAgICAgfQogICAgICAgICAgICAgdmVjW2orMV0gPSBhdXg7CiAgICAgICAgIH0KICAgICAgIH0KCiAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmcgYXJnc1tdKSB7CgogICAgICAgICAgICAgIC8vZGVjbGFyYW0gdW4gdmVjdG9yIGRlIGludHJlZ2kKICAgICAgICAgICAgICBpbnRbXSB2ZWN0b3IgPSB7Nyw2LDUsLTQsMywyLC0xLDB9OwoKICAgICAgICAgICAgICAvL2FmaXNhbSBwZSBlY3JhbiB1biBzdHJpbmcKICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oInZlY3RvcnVsIG9yaWdpbmFsIik7CgogICAgICAgICAgICAgIC8vYWZpc2FtIHZlY3RvcnVsIHBlIHN0ZG91dAogICAgICAgICAgICAgIERpc3BsYXlWZWN0b3IoIHZlY3RvciApOwoKICAgICAgICAgICAgICBJbnNlcnRTb3J0KCB2ZWN0b3IgKTsKCiAgICAgICAgICAgICAgLy9hZmlzYW0gcGUgZWNyYW4gdW4gc3RyaW5nCiAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ2ZWN0b3J1bCBzb3J0YXQiKTsKCiAgICAgICAgICAgICAgLy9hZmlzYW0gaW5jYSBvIGRhdGEgdmVjdG9ydWwgc29ydGF0CiAgICAgICAgICAgICAgRGlzcGxheVZlY3RvciggdmVjdG9yICk7CiAgICAgICB9CgogICAgICAgcHVibGljIHN0YXRpYyB2b2lkIERpc3BsYXlWZWN0b3IoaW50W10gdmVjKSB7CgogICAgICAgICAgZm9yKGludCB2YWx1ZTogdmVjKSB7CgogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCh2YWx1ZSArICIgIik7CiAgICAgICAgICB9CgogICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCk7CiAgICAgICB9Cn0KCi8qClZFQ1RPUjogNyA4IDEgNSA5CiAgICBpOiAgMCAxIDIgMyA0CiAgICBqID0gaSAtIDEKICAgIDwtLS0tCiAgICBmb3IoaSA9IDE7IGkgPCBsdW5naW1lX1ZFQ1RPUjsgKytpKSB7CiAgICAgICAgdGVtcCA9IHZlY1tpXQogICAgICAgIGogPSBpIC0gMTsvL2ogPSAwCiAgICAgICAgd2hpbGUoaiA+PSAwIFNJIHZlY1tqXSA+IHRlbXApIHsKICAgICAgICAgICB2ZWNbaisxXSA9IHZlY1tqXQogICAgICAgICAgai0tOwogICAgICAgICAgLy9qID0gaiAtMQogICAgICAgfSAgICAgIAogICAgICAgdmVjW2orMV0gPSBhdXg7CiAgICAgICAvL3ZlY1stMSsxXSA9IGF1eAogICAgICAgLy92ZWNbMF0gPSBhdXgKICAgIH0gCiov