Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Sorting

Sorting

Sorting is the process of rearranging a set of data into a specific order. It is one of the most fundamental and most thoroughly studied algorithmic problems in data processing.


The Sorting Algorithm Family

The Sorting Algorithm Family - Time Complexity and Feature Comparison

Basic Sorting (O(n^2))

Bubble sort, selection sort, and insertion sort are simple to implement, suitable for small-scale data or teaching.

Advanced Sorting (O(n log n))

Quick sort, merge sort, and heap sort are the most widely used in engineering. Shell sort is an improvement over insertion sort.

Example

#include

void

swap

(

int

a

,

int

b

)

{

int

t

=

a

;

a

=

b

;

b

=

t

;

}

/* Bubble sort - O(n^2), stable
   Each pass "bubbles" the largest element to the end */

void

bubbleSort

(

int

arr

[

]

,

int

n

)

{

   

for

(

int

i

=

0

;

i

<

n

1

;

i

++

)

{

       

int

swapped

=

0

;

 

/* Optimization: early termination */

       

for

(

int

j

=

0

;

j

<

n

1

i

;

j

++

)

{

           

if

(

arr

[

j

]

arr

[

j

1

]

)

{

                swap

(

&

arr

[

j

]

,

&

arr

[

j

1

]

)

;

                swapped

=

1

;

           

}

       

}

       

if

(

!

swapped

)

break

;

 

/* Already sorted, end early */

   

}

}

/* Quicksort partition function - O(n log n) average, unstable
   Choose pivot, place elements smaller than pivot on the left and larger on the right */

int

partition

(

int

arr

[

]

,

int

low

,

int

high

)

{

   

int

pivot

=

arr

[

high

]

;

 

/* Choose the last element as pivot */

   

int

i

=

low

1

;

       

/* i points to the end of the region smaller than pivot */

   

for

(

int

j

=

low

;

j

<

high

;

j

++

)

{

       

if

(

arr

[

j

]

<

pivot

)

{

i

++;

            swap

(

&

arr

[

i

]

,

&

arr

[

j

]

)

;

       

}

   

}

    swap

(

&

arr

[

i

1

]

,

&

arr

[

high

]

)

;

 

/* Place the pivot in its correct position */

   

return

i

1

;

}

void

quickSort

(

int

arr

[

]

,

int

low

,

int

high

)

{

   

if

(

low

<

high

)

{

int

pi

=

partition

(

arr

,

low

,

high

)

;

 

/* Partition index */

        quickSort

(

arr

,

low

,

pi

1

)

;

 

/* Recursively sort the left half */

        quickSort

(

arr

,

pi

1

,

high

)

;

 

/* Recursively sort the right half */

   

}

}

void

printArray

(

int

arr

[

]

,

int

n

)

{

   

for

(

int

i

=

0

;

i

<

n

;

i

++

)

printf

(

"%d "

,

arr

[

i

]

)

;

   

printf

(

"\n"

)

;

}

int

main

(

)

{

   

int

arr1

[

]

=

{

64

,

34

,

25

,

12

,

22

,

11

,

90

}

;

   

int

n1

=

7

;

    bubbleSort

(

arr1

,

n1

)

;

   

printf

(

"Bubble sort: "

)

;

printArray

(

arr1

,

n1

)

;

   

/* Output: 11 12 22 25 34 64 90 */

   

int

arr2

[

]

=

{

64

,

34

,

25

,

12

,

22

,

11

,

90

}

;

    quickSort

(

arr2

,

0

,

6

)

;

   

printf

(

"Quick sort: "

)

;

printArray

(

arr2

,

7

)

;

   

/* Output: 11 12 22 25 34 64 90 */

   

return

0

;

}


Illustration of the Divide-and-Conquer Idea

Three Major O(n log n) Sorting Algorithms - Divide-and-Conquer Strategy Comparison

Quick Sort (Partition)

Choose pivot

Smaller than pivot -> left, larger -> right

Recursively sort the left and right subarrays

Unstable | in-place | most commonly used

Merge Sort (Merge)

Recursively divide down to the smallest units

Merge two ordered subsequences

Needs an extra array O(n)

Stable | needs extra space | external sorting

Heap Sort (Heapify)

Build a max heap O(n)

Place the heap top (maximum) at the end

Re-adjust the heap O(log n)

Unstable | in-place O(1) | optimal space

Complete Sorting Algorithm Comparison

Algorithm Best Average Worst Space Stability
Bubble Sort O(n) O(n²) O(n²) O(1) Stable
Selection Sort O(n²) O(n²) O(n²) O(1) Unstable
Insertion Sort O(n) O(n²) O(n²) O(1) Stable
Quick Sort O(n log n) O(n log n) O(n²) O(log n) Unstable
Merge Sort O(n log n) O(n log n) O(n log n) O(n) Stable
Heap sort O(n log n) O(n log n) O(n log n) O(1) Unstable
Shell Sort O(n log n) O(n^1.3) O(n²) O(1) Unstable

Practical selection advice: for general scenarios prefer quick sort; if stability is needed choose merge sort; if memory is extremely limited choose heap sort; for small-scale or nearly ordered data choose insertion sort.