Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
#include <iostream>
using namespace std;
int main() {
int arr[] = {34, 12, 24, 65, 63, 22};
int arraySize = (sizeof(arr) / sizeof(*arr));
unsigned char buf[101]={0};
for (int k = 0; k < arraySize; k++) {
buf[arr[k]]++;
}
unsigned char i=0;
for (int k = 0; k <= 100; k++) {
arr[i]=k;
i+=buf[k];
}
for(int a:arr){
cout<<a<<endl;
}
return 0;
}

a = [5,7,3,2,7,9,8]
def bsort(x):
for i in range(len(x)):
for j in range(len(x)-1):
a = x[j]
b = x[j+1]
x[j] = int ((a+b)/2.0 - ((a-b)**2)**0.5/2.0)
x[j+1] = int ((a+b)/2.0 + ((a-b)**2)**0.5/2.0)
return x
print a
print bsort(a)
let ub = 100
let sort (input: _[]) =
let founds = Array.zeroCreate ub
input |> Array.iter (fun e -> founds.[e] <- 1)
let output = Array.zeroCreate (input.Length + 1)
founds
|> Seq.indexed
|> Seq.scan (fun (prevPos, _) (i, v) ->
let pos = prevPos + (1*v)
let target = pos*v
(pos, i)
) (0, 0)
|> Seq.iter (fun (pos, i) -> output.[pos] <- i)
output |> Array.skip 1
let sort (input: _[]) =
let founds = Array.zeroCreate ub
input |> Array.iter (fun e -> founds.[e] <- 1)
let output = Array.zeroCreate (input.Length + 1)
founds
|> Seq.indexed
|> Seq.scan (fun (prevPos, _, _) (i, v) ->
let pos = prevPos + (1*v)
let target = pos*v
(pos, target, i)
) (0, 0, 0)
|> Seq.iter (fun (_, pos, i) -> output.[pos] <- i)
output |> Array.skip 1
let sort input =
let founds = Array.zeroCreate ub
input |> Seq.iter (fun e -> founds.[e] <- founds.[e] + 1)
founds
|> Seq.indexed
|> Seq.collect (fun (i, v) -> Seq.replicate v i)
|> Seq.toArrayПоскольку я до этого жил в теплом мире фреймворков и библиотек
А если вы вкладываете условия в обходе массива в недопустимые, то задача не имеет решения.
int Array[100],Sum,Idx;
int Len=100;
int PC;
void Func_0(){
Sum=Idx=0;
PC=1;
}
void Func_1(){
PC=3+((Idx-Len)>>31); // 3 or 2
}
void Func_2(){
Sum+=A[Idx++];
PC=1;
}
void Func_3(){
printf("All done\n");
exit(0);
}
void (*(Func[]))()={Func_0,Func_1,Func_2,Func_3};
void MainLoop(){
PC=0;
for(;;) Func[PC]();
}
void sort(int *A,int N){
for(int a=0;a<N;a++){
for(int b=a+1;b<N;b++){
int s=((A[a]-A[b])>>31)&(A[a]^A[b]);
A[a]^=s;
A[b]^=s;
}
}
}
bool s = a[i] > a[j]; // true -> 1, false -> 0
a[i] = a[i] * !s + a[j] * s;
a[j] = a[i] * s + a[j] * !s;
if(условие){
какой-то код
}
while(условие){
какой-то код
break;
}
Сортировка без if-ов