Exemplo n.º 1
0
int main() {
  int A[10] = {2, 1, 5, 4, 6, 7, 9, 8,3, 0};
  int size = sizeof(A) / sizeof(int);
  quick_sort(A, 0, size-1);
  print(A, 0, 10);

  int B[0] = {};
  size = sizeof(B)/sizeof(int);
  quick_sort(B, 0, size-1);
  print(B, 0, size);

  int C[1] = {1};
  size = sizeof(C)/sizeof(int);
  quick_sort(C, 0, size-1);
  print(C, 0, size);


  int D[10] = {12, 32, 22, 22, 22, 12, -1, 12, 0, -100};
  size = sizeof(D)/sizeof(int);
  quick_sort(D, 0, size-1);
  print(D, 0, size);
  return 0;
}
Exemplo n.º 2
0
static void test3()  
{  
	int i;
	int array[] = {4, 3, 2,1,5,6,7,8,5,6,7,9,100,200,400, 19, 30, 20, 15};  
	quick_sort(array, sizeof(array)/sizeof(int));  
	assert(1 == array[0]);  
	assert(2 == array[1]);  
	assert(3 == array[2]);  
	assert(4 == array[3]);  
	printf("sorted test3 array: ");
	for(i = 0; i < sizeof(array)/sizeof(int); i++ )
		printf("%d ", array[i]);
	printf("\n");
}  
Exemplo n.º 3
0
void quick_sort(char *str, int left, int right){
	if(left < right){
		char pivot = median(str[left], str[(left + right) / 2], str[right]);
		int i = left;
		int j = right;
		while(1){
			while(str[i] < pivot) ++i;
			while(str[j] > pivot) --j;
			if(i < j)
				if(str[i] == str[j])
					--j;
				else
					swap(&str[i], &str[j]);
			else 
				break;

		}
		//	swap(&str[i], &str[right - 1]);
			quick_sort(str, left, i - 1);
			quick_sort(str, i + 1, right);

	}
}
Exemplo n.º 4
0
int main(void)
{
      int array[N];
      
      init_array(array,N);
printf("original\n");
      printf_array(array,N);
getchar();
//      bubble_array(array,N);
      quick_sort(array,0,N-1);
printf("sort\n");
      printf_array(array,N);
    return 0;
}
Exemplo n.º 5
0
int main()
{
	int size;
	printf("Enter the size of array : ");
	scanf("%d",&size);
	int arr[size],i;
	for(i=0;i<size;i++)
		scanf("%d",&arr[i]);
	quick_sort(arr,0,size-1);
	for(i=0;i<size;i++)
		printf("%d\t",arr[i]);
	printf("\n");
	return 0;
}
Exemplo n.º 6
0
/* Driver program to test above functions */
int main()
{
    int arr[] = {12, 11, 13, 5, 6, 7};
    int size = sizeof(arr)/sizeof(arr[0]);
 
    printf("Given array is \n");
    printArray(arr, size);
 
    quick_sort(arr, 0, size - 1);

   printf("\nSorted array is \n");
   printArray(arr, size);
   return 0;
}
int main()
{
	int array[] = {3, 7, 2, 4, 1, 6};
	int ilen = LENGTH(array);
	
	printf("Begin sort:\n");
  	print_array(array, ilen);
	
	quick_sort(array, 0, ilen-1);
	
	printf("After sort: \n");
	print_array(array, ilen);

}
Exemplo n.º 8
0
void test_quick_sort(void)
{
	int quick_sort_array[] = {9,8,7,6,5,4,3,2,1};
	int i;

	quick_sort(quick_sort_array, 0, 8);

	debug_print("quick sort test:");
	for (i = 0; i < 9; i++)
	{
		debug_print("%d ", quick_sort_array[i]);
	}

}
Exemplo n.º 9
0
//Função do quicksort, que recebe o limite
void quick_sort (int *nArray, int nLimite) {
  int nAnte, nProx, nMetade, nValAux, nAux;
     
  //Testando o limite e pegando a metade
  if (nLimite < 2)
    return;
  nMetade = nArray[nLimite / 2];
     
  //fazendo um for duplo, diminuindo o próximo e aumentando o anterior
  for (nAnte = 0, nProx = nLimite - 1;; nAnte++, nProx--) {
    //imprimindo os valores
    for(nAux=0;nAux<=nLimite-1;nAux++){
      printf("[%d]",nArray[nAux]);
      espera;
    }
    printf("\n");
         
    //enquanto for menor que a metade
    while (nArray[nAnte] < nMetade)
      nAnte++;
    //enquanto a metade for menor que o próximo
    while (nMetade < nArray[nProx])
      nProx--;
    //se o anterior é maior que o próximo quebra o laço
    if (nAnte >= nProx)
      break;
     
    //fazendo troca de posições
    nValAux = nArray[nAnte];
    nArray[nAnte] = nArray[nProx];
    nArray[nProx] = nValAux;
  }
   
  //Chamando rotina novamente em recursividade
  quick_sort(nArray, nAnte);
  quick_sort(nArray + nAnte, nLimite - nAnte);
}
Exemplo n.º 10
0
Arquivo: sort.c Projeto: bazz2/utils
void quick_sort(int a[], int left, int right)
{
    int i = left;
    int j = right;
    int pivot = a[left];

    if (left >= right)
        return;

    while (i != j) {
        /* process right zone */
        while (i < j && a[j] >= pivot)
            j--;
        if (i < j) {
            a[i] = a[j];
            printf("a[%d] = a[%d]\n", i, j);
        }

        /* process left zone */
        while (i < j && a[i] <= pivot)
            i++;
        if (i < j) {
            a[j] = a[i];
            printf("a[%d] = a[%d]\n", j, i);
        }
    }
    a[i] = pivot;
    printf("a[%d] = %d\n", i, pivot);

    int idx;
    for (idx = 0; idx < 9; idx++)
        printf("%d ", a[idx]);
    printf("\n");

    quick_sort(a, left, i-1);
    quick_sort(a, i+1, right);
}
Exemplo n.º 11
0
// greedy methods
void quick_sort(item* arr, int from, int to)
{
	int i = from,
		j = to;
	double x = arr[(from + to) / 2].cost / arr[(from + to) / 2].weight; //опорный элемент

	do // пока i и j не пересеклись
	{
		while ((arr[i].cost / arr[i].weight) < x) i++; // идём до элемента не из левой группы (больше разделительного)
		while ((arr[j].cost / arr[j].weight) > x) j--; // идём, пока не встретим элемент меньше разделительного

		if (i <= j)
		{
			if ((arr[i].cost / arr[i].weight) > (arr[j].cost / arr[j].weight)) // строгое для устойчивости
			{
				//swap(arr[i], arr[j]);
				int temp = arr[i].cost;
				arr[i].cost = arr[j].cost;
				arr[j].cost = temp;
				temp = arr[i].weight;
				arr[i].weight = arr[j].weight;
				arr[j].weight = temp;
			}
			i++;
			j--;
		}
	} while (i <= j);

	if (from < j) // рекурсивный вызов для левой части массива
	{
		quick_sort(arr, from, j);
	}
	if (to > i) // рекурсивный вызов для правой части массива
	{
		quick_sort(arr, i, to);
	}
}
Exemplo n.º 12
0
void quick_sort(int* data, int length)
{
    int right = length, left = 0;
    int middle = data[length/2];

    do
    {
        while (data[left] < middle)
        {
            left++;
        }
        while (data[right] > middle)
        {
            right--;
        }

        if (left <= right)
        {
            swap1(&data[left], &data[right]);
            left++;
            right--;
        }

    }
    while (left < right);

    if (right > 0)
    {
        quick_sort(data, right);
    }

    if (left < length)
    {
        quick_sort(data + left, length - left);
    }

}
Exemplo n.º 13
0
void quick_sort(int left, int right, long ar[], int ar_len[]) {

/*quick_sortは整数が格納された配列ar、各整数の文字数が格納された配列ar_lenを*/
/*呼び出し元の関数から受け取り、配列中の左端left、右端rightで指定される範囲に対して*/
/*クイックソートによるar,ar_lenの整列を行う。*/

  int p_index, p_val;		/*p_indexは軸要素の添え字番号、p_valは軸要素そのものを格納する。*/
  int l_edge = left;		/*処理範囲の軸要素より左の範囲を調査するカーソル*/
  int r_edge = right;		/*処理範囲の軸要素を含む、軸要素より右の範囲を調査するカーソル*/

  p_index = pivot(left, right, ar);	/*配列中の軸要素の要素番号を求める*/

  if(p_index != -1) {		/*p_indexが-1ならば、pivotで調べた範囲はすべて同じ数字が入っているのでソートをスキップ*/
    p_val = ar[p_index];	/*軸要素をp_valに格納*/

    while(1) {	/*配列の分割を行うループ*/

/*l_edgeは処理する範囲の左端から、軸要素より大きな要素を見つけるまで右にずれる。*/
/*r_edgeは処理する範囲の右端から、軸要素より小さな要素を見つけるまで左にずれる。*/
      while(ar[l_edge] < p_val) l_edge++;
      while(ar[r_edge] >= p_val) r_edge--;

/*l_edgeがr_edgeより右なら、その範囲でのソートは終了。*/
      if(l_edge > r_edge) break;

/*ソートが終わっていないなら、その時点でのl_edgeとr_edgeの位置にある要素を交換する。*/
      swap(r_edge, l_edge, ar, ar_len);

/*交換後、l_edge、r_edgeの両方をずらして、ループする。*/
      l_edge++;
      r_edge--;
    }
/*調査範囲の分割後、分割した左の範囲と右の範囲についてそれぞれquick_sortを再帰呼び出ししてソートを進める*/
    quick_sort(left, l_edge - 1, ar, ar_len);
    quick_sort(l_edge, right, ar, ar_len);
  }
}
Exemplo n.º 14
0
//-------- Begin of function GameFileArray::save_new_game -----//
//
// Save current game to a new saved game file immediately without
// prompting menu.
//
// Called by GameFileArray::process_action() and error handler.
//
// [char*] fileName - file name of the saved game
//
void GameFileArray::save_new_game(const char* fileName)
{
	GameFile  gameFile;
	GameFile* gameFilePtr;
	int       addFlag=1;
	int       gameFileRecno;

	memset( &gameFile, 0, sizeof(GameFile) );

	if( fileName )
	{
		//----- check for overwriting an existing file ----//

		for( gameFileRecno=1 ; gameFileRecno<=game_file_array.size() ; gameFileRecno++ )
		{
			gameFilePtr = game_file_array[gameFileRecno];

			if( strcmp(gameFilePtr->file_name, fileName)==0 )      // if this file name already exist
			{
				addFlag=0;
				break;
			}
		}

		strcpy( gameFile.file_name, fileName );
   }
	else
   {
      gameFile.set_file_name();        // give it a new game_file_name based on current group name
   }

	//----------- save game now ------------//

	if( gameFile.save_game(fileName) )
	{
		strcpy( last_file_name, gameFile.file_name );

		if( addFlag )
		{
         linkin(&gameFile);

			quick_sort( sort_game_file_function );
      }
		else
      {
         game_file_array.update(&gameFile, gameFileRecno);
      }
   }
}
Exemplo n.º 15
0
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int input[n];
        for(int i=0; i<n; i++)
            scanf("%d",&input[i]);
        quick_sort(0,n-1,n,input);
        for(int i=0; i<n; i++)
            printf("%d ",input[i]);
        printf("\n");
    }
    return 0;
}
Exemplo n.º 16
0
void quick_sort(float *a, int n)
{
  if (n < 2) {
    return;
  }
  float p = a[n / 2];
  float *l = a;
  float *r = a + n - 1;
  while (l <= r) {
    if (*l < p) {
      l++;
      continue;
    }
    if (*r > p) {
      r--;
      continue; // we need to check the condition (l <= r) every time we change the value of l or r
    }
    float t = *l;
    *l++ = *r;
    *r-- = t;
  }
  quick_sort(a, r - a + 1);
  quick_sort(l, a + n - l);
}
Exemplo n.º 17
0
int main(int argc, const char *argv[])
{
    int array[N];
    init_array(array, N);
    printf("The result :");
    getchar();
    print_array(array, N);
//    paixu_array(array, N);
//    xuan_array(array, N);
    quick_sort(array, 0, N-1);
    printf("<---------------------------------------------->");
    getchar();
    print_array(array, N);
    return 0;
}
Exemplo n.º 18
0
int main(void)
{
    int a[N];

    init(a, N);
    show(a, N);
   // bubble_sort(a, N);
   //select_sort(a, N);
   //insert_sort(a, N);
   //merge_sort(a, 0, N-1);
   quick_sort(a, 0, N-1);
    show(a, N);

    return 0;
}
Exemplo n.º 19
0
int main()
{
	int a[] = {1, 4, 3, 12, 0, 15, 9};
	int i;

	quick_sort(a, 0, LEN(a) - 1);

	for (i = 0; i < LEN(a); i++)
	{
		printf("%d  ", a[i]);
	}
	printf("\n");

	return 0;
}
Exemplo n.º 20
0
void quick_sort(int *arr, int left, int right)
{
    if (left > right)
    {
        return;
    }
    int i = left;
    int j = right;
    int t = 0;
    int tmp = arr[left];

    while (i != j)
    {
        while (tmp <= arr[j] && j > i)
        {
            --j;
        }
        while (tmp >= arr[i] && j > i)
        {
            ++i;
        }
        
        if (i < j)
        {
            t = arr[i];
            arr[i] = arr[j];
            arr[j] = t;
        }
    }
    arr[left] = arr[i];
    arr[i] = tmp;

    quick_sort(arr, left, i-1);
    quick_sort(arr, i+1, right);
    return;
}
Exemplo n.º 21
0
void quick_sort(int s,int e)
{
	int i,j,mid,temp;
	if(s<e)
	{
		i=s;
		j=e;
		mid=(i+j)/2;
		temp=p[mid];
		p[mid]=p[i];
		while(i<j)
		{
			while(i<j&&p[j]>=temp)
				j--;
			p[i]=p[j];
			while(i<j&&p[i]<=temp)
				i++;
			p[j]=p[i];
		}
		p[i]=temp;
		quick_sort(s,i-1);
		quick_sort(i+1,e);
	}
}
Exemplo n.º 22
0
int main(const int argc, const char *argv[]) {

	int i = 0;
	int arr [] = { 2, 3, 4, 1, 3, 6, 29, 2, 3, 7 };
	printf("Running quick sort example\n");
	quick_sort(arr, 0, 9);
	
	for (i = 0; i < 10; i++) {
		printf(" index%d: %d\n", i, arr[i]);
	}
	
	printf("Done\n");
	
	return 0;
}
Exemplo n.º 23
0
int main(void)
{
	char arr[100];
	int n = 100;

	memset(arr,0,n);
	_printf(arr,n);
	generate_array(arr, n);

	quick_sort(arr,0,99);

	_printf(arr,n);

	printf("hello\n");
}
Exemplo n.º 24
0
Arquivo: sort.c Projeto: nson7/ye
void quick_sort(int *a, size_t left, size_t right)
{
    if (left >= right)
        return ;

    int i = left;
    int j = right;
    int key = a[i];

    while (i < j)
    {
        while ((i < j) && (key <= a[j]))
            j--;
        a[i] = a[j];

        while ((i < j) && (key >= a[i]))
            i++;
        a[j] = a[i];
    }

    a[i] = key;
    quick_sort(a, left, i - 1);
    quick_sort(a, i + 1, right);
}
Exemplo n.º 25
0
/*
 * Code example taken from rosetta stone
 */
void quick_sort (int *a, int n) {
    if (n < 2)
        return;
    int p = a[n / 2];
    int *l = a;
    int *r = a + n - 1;
    while (l <= r) {
        if (*l < p) {
            l++;
        }
        else if (*r > p) {
            r--;
        }
        else {
            int t = *l;
            *l = *r;
            *r = t;
            l++;
            r--;
        }
    }
    quick_sort(a, r - a + 1);
    quick_sort(l, a + n - l);
}
Exemplo n.º 26
0
int quick_sort(int sortArr[10], int strtIdx, int endIdx){  // startIndex endIndex

  if(strtIdx>=endIdx)
    return 0;

  int i, j=strtIdx, pivot, tempVal; // i: array marker, j: swap position marker
  pivot = sortArr[strtIdx]; // selects the first element as pivot
  for(i=strtIdx+1;i<=endIdx;i++)
  {
    // if element is greater than pivot, swap them and moves marker upward.
    if(pivot>=sortArr[i])
    {
      tempVal = sortArr[j];
      sortArr[j] = sortArr[i]; // swap value
      sortArr[i] = tempVal;
      j++;
    }
  }
  // if final marker is larger than the last element (pivot), swap their position.
  if(sortArr[j]>sortArr[i-1])
  {
    tempVal = sortArr[j];
    sortArr[j] = sortArr[i-1]; // swap value
    sortArr[i-1] = tempVal;
  }

  /*printf("Sorted array:\n |");
  for(i=0;i<10;i++){
    printf(" %d |", sortArr[i]);
  }
  printf("\n\n");*/

  quick_sort(sortArr, strtIdx, j-1);
  quick_sort(sortArr, j+1, endIdx);

}
Exemplo n.º 27
0
int quick_sort(int array[], int length)
{
  if (length > 1) {
    int i = 0, j = length - 2;
    int median = get_median(array, length);

    print_array(array, length);
    while (1) {
      while (array[++i] < median) { }
      while (array[--j] > median) { }
      if (i < j) {
        swap(array, i, j);
      } else {
        break;
      }
    }
    swap(array, i, length - 1);

    quick_sort(array, i);
    quick_sort(array + i + 1, length - i - 1);
  }

  return 0;
}
Exemplo n.º 28
0
static void quick_sort(int32_t* array,int32_t left,int32_t right)
{
    if(right > left)
    {
        int32_t pivot = getPivot(array,left,right);
        int32_t i     = left;
        int32_t j     = right;

        if(pivot != -1)
        {
            while(1)
            {
                while(array[i] < pivot) i++;
                while(array[j] > pivot) j--;
                if(i >= j) break;
                SWAP(int32_t,array[i],array[j]);
                i++;
                j--;
            }
        }
        if(left < i - 1) quick_sort(array,left,i -1);
        if(j+1 < right) quick_sort(array,j+1,right);
    }
}
Exemplo n.º 29
0
////////////////////////
//Test Quick sorts
///////////////////////
void testQuickSort() {
   printf("\nNow sorting by quick sort>>>>>>>>>>\n");

   startProfileTime();
   randomize_in_place(A, ARRAY_SIZE);
   print_array(A, ARRAY_SIZE);
   endProfileTime("Gen random array");

   startProfileTime();
   quick_sort(A, 1, ARRAY_SIZE);
   endProfileTime("quick sort");

   printf("\nSorted array is :  ");
   print_array(A, ARRAY_SIZE);
}
Exemplo n.º 30
0
void quick_sort(int low,int high,int *a)
{
    int key = a[low];
    int i=low;
    int j=high;
    if(low<high)
    {
        while(low<high)
        {
            while(low<high && a[high]>key)
                high--;
            if(low<high)
                a[low]=a[high];
            while(low<high && a[low]<key)
                low++;
            if(low<high)
                a[high]=a[low];
        }
        a[low]=key;
        quick_sort(i,low-1,a);
        quick_sort(low+1,j,a);
    }

}