50174. Bubble Sort

難度:3/5

用unix系統main會生出不對的output,交上去就對了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>
// #include <inttypes.h>
#include "BubbleSort.h"

static inline int get_bit(uint64_t* n, int i){
return ((*n) >> (63 - i)) & 1;
}
static inline void toggle_bit(uint64_t* n, int i){
(*n) ^= ((uint64_t)(1) << (63 - i));
}

uint64_t step(uint64_t n){
for(int i = 0; i < 63; i++){
if(get_bit(&n, i) == 1 && get_bit(&n, i + 1) == 0){
toggle_bit(&n, i);
toggle_bit(&n, i + 1);
}
}
return n;
}

void BubbleSort(uint64_t n, uint64_t output[63]){
for(int i = 0; i < 63; i++){
n = step(n);
output[i] = n;
}
}


50174. Bubble Sort
https://aaronlin1229.github.io/judgegirl_50174/
Author
Akizumi
Posted on
July 17, 2023
Licensed under