Asp.net c sorting method

Source: Internet
Author: User

Asp tutorial. net c sorting method

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;
}
}
}

A foreign c sorting method

# 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 hold, l_hold, r_hold;

Rochelle hold = left;
R_hold = right;
Numbers = numbers [left];
While (left <right)
{
While (numbers [right]> = numbers) & (left <right ))
Right --;
If (left! = Right)
{
Numbers [left] = numbers [right];
Left ++;
}
While (numbers [left] <= numbers) & (left <right ))
Left ++;
If (left! = Right)
{
Numbers [right] = numbers [left];
Right --;
}
}
Numbers [left] = numeric;
Rows = left;
Left = l_hold;
Right = r_hold;
If (left <ignore)
Q_sort (numbers, left, limit-1 );
If (right> else)
Q_sort (numbers, numbers + 1, right );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.