Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define N 6
- void swap(void* a, void* b, size_t size) {
- void* tmp = malloc(size);
- if (tmp == NULL) {
- perror("");
- exit(1);
- }
- memcpy(tmp, a, size);
- memcpy(a, b, size);
- memcpy(b, tmp, size);
- free(tmp);
- }
- int moveToBeg(int n, int v[]) {
- int le = 0, ri = n - 1;
- while (le < ri) {
- while (v[le] < 0 && le < ri) {
- le++;
- }
- while (!(v[ri] < 0) && le < ri) {
- ri--;
- }
- if (le < ri) {
- swap(&v[le], &v[ri], sizeof(int));
- le++, ri--;
- }
- }
- return le;
- }
- void quickSort(int v[], int le, int ri) {
- if (le < ri) {
- int mid = (le + ri) / 2;
- swap(&v[le], &v[mid], sizeof(int));
- int i = le, j = ri, d = 0;
- while (i < j) {
- if (v[i] > v[j]) {
- swap(&v[i], &v[j], sizeof(int));
- d = 1 - d;
- }
- i += d;
- j -= 1 - d;
- }
- quickSort(v, le, i - 1);
- quickSort(v, i + 1, ri);
- }
- }
- void printArr(int n, int v[]) {
- for (int i = 0; i < n; i++) {
- printf("%d ", v[i]);
- }
- puts("");
- }
- int main(void) {
- int v[N] = {-2, 3, 4, -3, 1, -1};
- int fiPos = moveToBeg(N, v);
- printArr(N, v);
- quickSort(v, fiPos, N - 1);
- printArr(N, v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement