指引网

当前位置: 主页 > 编程开发 > C >

asp.net c排序方法

来源:网络 作者:佚名 点击: 时间:2017-07-19 23:04
[摘要] 

asp教程.net c排序方法

namespace quicksort
{
class quicksort
{
public static void sort(int[] array)
{
dosort(array,0, array.length-1);
}
private static void dosort( int[] array, int start, int end)
{
if( start < end)
{
int temp = partition(array, start, end);
dosort(array, start, temp-1);
dosort(array, temp + 1, end);
}
}
private static int partition(int[] array,int start, int end)
{
int index = start - 1;
for( var i=start; i< end; i++)
{
if( array[i] < array[end])
{
index++;
swap(array, index, i);
}
}
swap(array, index +1, end);
return index + 1;
}
private static void swap(int[] array, int index1, int index2)
{
var temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
}
}

国外的一款c 排序方法

#include <stdlib.h>
#include <stdio.h>

#define num_items 100

void quicksort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);

int numbers[num_items];


int main()
{
  int i;

  //seed random number generator
  srand(getpid());

  //fill array with random integers
  for (i = 0; i < num_items; i++)
    numbers[i] = rand();

  //perform quick sort on array
  quicksort(numbers, num_items);

  printf("done with sort.n");
  for (i = 0; i < num_items; i++)
    printf("%in", numbers[i]);
}


void quicksort(int numbers[], int array_size)
{
  q_sort(numbers, 0, array_size - 1);
}

 

void q_sort(int numbers[], int left, int right)
{
  int pivot, l_hold, r_hold;

  l_hold = left;
  r_hold = right;
  pivot = numbers[left];
  while (left < right)
  {
    while ((numbers[right] >= pivot) && (left < right))
      right--;
    if (left != right)
    {
      numbers[left] = numbers[right];
      left++;
    }
    while ((numbers[left] <= pivot) && (left < right))
      left++;
    if (left != right)
    {
      numbers[right] = numbers[left];
      right--;
    }
  }
  numbers[left] = pivot;
  pivot = left;
  left = l_hold;
  right = r_hold;
  if (left < pivot)
    q_sort(numbers, left, pivot-1);
  if (right > pivot)
    q_sort(numbers, pivot+1, right);
}

------分隔线----------------------------