#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void dispArrayLine(int a[], int length, int dispTotal){
int total = 0;
for(int i=0; i<length; i++){
total += a[i];
printf("%d", a[i]);
if(i!=length-1){
printf(" ");
}else{
if(dispTotal){
printf(" : %d", total);
}
printf("\n");
}
}
}
void dispArrayList(int a[], int length){
for(int i=0; i<length; i++){
printf("[%d] : %d\n", i, a[i]);
}
}
void resetArray(int a[], int length){
for(int i=0; i<length; i++){
a[i] = 0;
}
}
void init(){
srand((unsigned)time(NULL));
}
double getRandomDouble(){
return (double)rand() / RAND_MAX;
}
int getRandomInt(int min, int max){
double d = getRandomDouble();
int randomOffset = d * (max - min + 1);
return min + randomOffset;
}
void setRandomIntForArray(int a[], int length, int min, int max){
for(int i=0; i<length; i++){
a[i] = getRandomInt(min, max);
}
}
int getPivot(int a, int b, int c){
if(a > b){
if(b > c){
return b;
}else{
return a > c ? c : a ;
}
}else{
if(a > c){
return a;
}else{
return b > c ? c : b ;
}
}
}
int getMax(int a, int b, int c){
if(a > b){
return a > c ? a : c ;
}else{
return b > c ? b : c ;
}
}
int getMin(int a, int b, int c){
if(a < b){
return a < c ? a : c ;
}else{
return b < c ? b : c ;
}
}
void swap(int a[], int l, int r){
int t = a[l];
a[l] = a[r];
a[r] = t;
}
void quicksort_core(int a[], int length, int left, int right){
if(right <= left){
return;
}
int l = left;
int r = right;
int p = getPivot(a[l], a[l+1], a[r]);
while(1){
while(a[l]<p){
l++;
}
while(p<a[r]){
r--;
}
if(r<=l){
break;
}
swap(a, l, r);
l++;
r--;
}
quicksort_core(a, length, left, l-1);
quicksort_core(a, length, r+1, right);
}
void quicksort(int a[], int length){
int left = 0;
int right = length-1;
quicksort_core(a, length, left, right);
}
int main(void){
init();
int length = 30;
int a[length];
int min = -9;
int max = 9;
setRandomIntForArray(a, length, min, max);
dispArrayLine(a, length, 1);
quicksort(a, length);
dispArrayLine(a, length, 1);
return 0;
}