public class Main {
public static void _QuickSort(int lo, int hi, int[] vec) {
int i=lo,
j=hi,
pivot = vec[(hi+lo)>>1];
while(i<=j) {
while(vec[i]<pivot){ i++; }
while(vec[j]>pivot){ j--; }
if(i<=j){
int tmp = vec[i];
vec[i]=vec[j];
vec[j]=tmp;
i++;j--;
}
}
if(lo<j) _QuickSort(lo,j,vec);
if(i<hi) _QuickSort(i,hi,vec);
}
public static void QuickSort(int[] vec) {
_QuickSort(0, vec.length - 1, vec);
}
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 );
QuickSort( 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
+ " "); }
}
}
cHVibGljIGNsYXNzIE1haW4gewoKICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBfUXVpY2tTb3J0KGludCBsbywgaW50IGhpLCBpbnRbXSB2ZWMpIHsKCiAgICAgICAgIGludCBpPWxvLAogICAgICAgICAgICAgaj1oaSwKICAgICAgICAgICAgIHBpdm90ID0gdmVjWyhoaStsbyk+PjFdOwogICAgICAgICAgICAgCiAgICAgICAgIHdoaWxlKGk8PWopIHsKICAgICAgICAgICB3aGlsZSh2ZWNbaV08cGl2b3QpeyBpKys7IH0KICAgICAgICAgICB3aGlsZSh2ZWNbal0+cGl2b3QpeyBqLS07IH0KICAgICAgICAgICBpZihpPD1qKXsKICAgICAgICAgICAgIGludCB0bXAgPSB2ZWNbaV07CiAgICAgICAgICAgICAgICAgdmVjW2ldPXZlY1tqXTsKICAgICAgICAgICAgICAgICB2ZWNbal09dG1wOwogICAgICAgICAgICAgaSsrO2otLTsKICAgICAgICAgICB9CiAgICAgICAgIH0KICAgICAgICAgaWYobG88aikgX1F1aWNrU29ydChsbyxqLHZlYyk7CiAgICAgICAgIGlmKGk8aGkpIF9RdWlja1NvcnQoaSxoaSx2ZWMpOwogICAgICAgfQoKICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBRdWlja1NvcnQoaW50W10gdmVjKSB7CgogICAgICAgICAgICAgIF9RdWlja1NvcnQoMCwgdmVjLmxlbmd0aCAtIDEsIHZlYyk7CiAgICAgICB9CgogICAgICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nIGFyZ3NbXSkgewoKICAgICAgICAgICAgICAvL2RlY2xhcmFtIHVuIHZlY3RvciBkZSBpbnRyZWdpCiAgICAgICAgICAgICAgaW50W10gdmVjdG9yID0gezcsNiw1LC00LDMsMiwtMSwwfTsKCiAgICAgICAgICAgICAgLy9hZmlzYW0gcGUgZWNyYW4gdW4gc3RyaW5nCiAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ2ZWN0b3J1bCBvcmlnaW5hbCIpOwoKICAgICAgICAgICAgICAvL2FmaXNhbSB2ZWN0b3J1bCBwZSBzdGRvdXQKICAgICAgICAgICAgICBEaXNwbGF5VmVjdG9yKCB2ZWN0b3IgKTsKCiAgICAgICAgICAgICAgUXVpY2tTb3J0KCB2ZWN0b3IgKTsKCiAgICAgICAgICAgICAgLy9hZmlzYW0gcGUgZWNyYW4gdW4gc3RyaW5nCiAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ2ZWN0b3J1bCBzb3J0YXQiKTsKCiAgICAgICAgICAgICAgLy9hZmlzYW0gaW5jYSBvIGRhdGEgdmVjdG9ydWwgc29ydGF0CiAgICAgICAgICAgICAgRGlzcGxheVZlY3RvciggdmVjdG9yICk7CiAgICAgICB9CgogICAgICAgcHVibGljIHN0YXRpYyB2b2lkIERpc3BsYXlWZWN0b3IoaW50W10gdmVjKSB7CgogICAgICAgICAgZm9yKGludCB2YWx1ZTogdmVjKSB7CgogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludCh2YWx1ZSArICIgIik7CiAgICAgICAgICB9CgogICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCk7CiAgICAgICB9Cn0=