Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- 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);
- }
- #define NAME_MAX 35
- typedef struct {
- char name[NAME_MAX];
- int salary;
- bool bonus;
- } Emp;
- int moveToBeg(int n, Emp emps[]) {//O(n)
- int le = 0, ri = n - 1;
- while (le < ri) {
- while (emps[le].bonus && le < ri) {
- le++;
- }
- while (!emps[ri].bonus && le < ri) {
- ri--;
- }
- if (le < ri) {
- swap(&emps[le], &emps[ri], sizeof(Emp));
- le++, ri--;
- }
- }
- return le;
- }
- void printEmp(const Emp *const e) {
- printf("{%s %d %d}\n", e->name, e->salary, e->bonus);
- }
- void printEmpArr(int n, const Emp e[]) {
- for (int i = 0; i < n; i++) {
- printEmp(&e[i]);
- }
- }
- void quickSort(Emp emps[], int le, int ri) {
- if (le < ri) {
- int mid = (le + ri) / 2;
- swap(&emps[mid], &emps[le], sizeof(Emp));
- int i = le, j = ri, d = 0;
- while (i < j) {
- if (emps[j].salary > emps[i].salary) {
- swap(&emps[i], &emps[j], sizeof(Emp));
- d = 1 - d;
- }
- i += d;
- j -= 1 - d;
- }
- quickSort(emps, le, i - 1);
- quickSort(emps, i + 1, ri);
- }
- }
- int main(void) {
- const int n = 6;
- Emp emps[] = {
- {"john1", 4321, true},
- {"john2", 43, false},
- {"john3", 3, false},
- {"john4", 3222, true},
- {"john5", 213, false},
- {"john6", 333, false},
- };
- //printEmpArr(n, emps);
- int j = moveToBeg(n, emps);
- printEmpArr(n, emps);
- puts("-----------------------");
- printf("%d\n", j);
- quickSort(emps, j, n - 1);
- printEmpArr(n, emps);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement