-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
73 lines (62 loc) · 1.12 KB
/
sort.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include "sort.h"
void quick_sort (int *a, int size) {
q_sort(a, 0, size);
}
void q_sort(int *a, int left, int right){
int pivot, l, r, loops=0;
l = left;
r = right;
pivot = a[left];
while (left < right)
{
while ((a[right] >= pivot) && (left < right)){
right--;
loops++;
}
if (left != right)
{
a[left] = a[right];
left++;
}
while ((a[left] <= pivot) && (left < right)){
left++;
loops++;
}
if (left != right)
{
a[right] = a[left];
right--;
}
}
a[left] = pivot;
pivot = left;
left = l;
right = r;
if (left < pivot)
q_sort(a, left, pivot-1);
if (right > pivot)
q_sort(a, pivot+1, right);
}
int removeDuplicates(int* A, int size) {
int j = 0;
int i = 1;
while (i < size) {
if (A[i] == A[j]) {
i++;
} else {
j++;
A[j] = A[i];
i++;
}
}
return j + 1;
}
int write_all ( int fd , void * buff , size_t size ) {
int sent , n ;
for ( sent = 0; sent < size ; sent += n ) {
if (( n = write ( fd , buff + sent , size - sent ) ) == -1)
return -1; /* error */
}
return sent ;
}