0% found this document useful (0 votes)
79 views4 pages

Radix Shell Shaker Sort (AutoRecovered)

Uploaded by

Lê Ngọc Bảo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views4 pages

Radix Shell Shaker Sort (AutoRecovered)

Uploaded by

Lê Ngọc Bảo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Radix Sort

Ideas:

It is a linear sorting algorithm that sorts elements by processing them digit by


digit. It seperates the elements into buckets based on each digit’s value. By
repeatedly sorting the elements by their significant digits, from the least
significant to the most significant.

Step:

Step 1: Find the largest element in the array. Then we declare a variable divid
that start from 1
Step 2: We use while loop with condition which stops when the largest element
divide variable divid <= 0. Then declare an array with 10 values from 0 to 9 and
an temporary array to save the sorted array.
Step 3: Sort the elements based on the unit place digits. We use a stable sorting
technique, such as counting sort, to sort the digits at each significant place. Then
multiply variable divid by 10.
Step 4: Repeat step 2 to sort next digit to the left until the largest element divide
variable divid <= 0
Step 5: The array is now sorted in ascending order.

DATA STRUCTURES AND ALGORITHM PROJECT

Time Complexity

- Best case: O(nk)


- Average case: O(nk)
- Worst case: O(nk)
- Space complexity: O(n + k)

Advantages

▪ linear time complexity


▪ stable sorting algorithm

▪ efficient for sorting large numbers of values

▪ easily parallelized

Disadvantages

▪ Not efficient for sorting floating-point numbers

▪ requires a significant amount of memory

▪ not efficient for small data sets

Shell Sort

Ideas:

arrange the list of items so that, starting anywhere, taking every h th element produces a sorted list.

 Such a list is said to be h-sorted or h interleaved lists.

 h is set from large values to small ones.

 h = 1 guarantees to leave a sorted list in the end

Step:

1. Initially, gap = n/2

2. Divide the array into gap sub-arrays with intervals between each item equal to gap.

3. Sort each sub-array using the insertion sort algorithm

4. Reduce the gap by half

5. Repeat step 2-4 until gap = 1

6. Sort the entire array using insertion sort.

Time Complexity

- Best case: O()


- Average case: O()
- Worst case: O()
- Space complexity: O()
Advantages and Disadvantages

Advantages

 Efficient for medium-sized data sets.


 Adaptive, meaning that it can adjust its performance depending on the input
data.
 In-place sorting, meaning that it does not require additional memory space
for sorting.
Disadvantages:
 The complexity of implementation.

 Not stable, meaning that it does not preserve the order of equal elements in
the input data.
 Non-optimal worst-case performance of O(n^2)

Shaker Sort

Ideas:

It extends bubble sort by operating in two directions.

Step:

1. Initialize head from beginning of array and tail from the end of it

2. Then we use a while loop with condition head < tail and traverse simultanously both from left to
right and opposite of the array

 In the for loop from left to right: if the value of index i > index (i + 1),
then we will swap 2 values of them

 In the for loop from left to right: if the value of index i < index (i - 1),
then we will swap 2 values of them

After 2 loops, we will increase value of head and decrease value of tail

3. Repeat step 2 until head >= tail then we will stop the while loop
Time Complexity

- Best case: O(n)


- Average case: O(n^2)
- Worst case: O(n^2)
- Space complexity: O()

Advantages and Disadvantages

Advantages:
1. More efficient than bubble sort in certain cases, especially when the
array being sorted has a small number of unsorted elements near the
end.
2. Simple to understand and implement
Disadvantages:
1. Can be slow for large datasets or datasets that are already partially
sorted.
2. Requires additional variables to keep track of the starting and ending
indices of the subarrays being sorted in each traverse, which make it
less efficient in terms of memory usage than other sorting algorithms.

You might also like