0% found this document useful (0 votes)
23 views

exchange sort

Uploaded by

rregmi967
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

exchange sort

Uploaded by

rregmi967
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/31/2024

 An array A[ ] of N numbers is sorted in


ascending order if the array entries increase (or
never decrease) as indices increase:

A[0]  A[1]    A[ N  2]  A[ N  1]

• We assume that each element to be sorted


The Sorting Problem contains a key field which is comparable to
all other key fields in the collection

• Many algorithms have been developed to


solve the problem

 Under most circumstances, the least


efficient of all sorting algorithms, because
of the excessive number of swap operations
 Exchange sort requires n-1 major loop
iterations, and up to n-1 swaps in each
iteration, resulting in a worst case of ~n2
element swaps!!

1
1/31/2024

Selection sort
Walk from left to right through the array.
On each step, select the element that goes
in the current location in sorted order and
In each iteration, selection sort put it there.

Finds the smallest entry in the unsorted


After k steps, the first k elements are in
 sorted order and are in their final
portion of the array, and positions.
k=1 k=2 k=3 k=4
 Swaps it with the element in the
corresponding position.

k=5 k=6 k=N-1

...
5

Selection Sort (p. 292)


Selection sort (Java version)
8 16 4 8 12 6 4 10 16 2 18 14
public void selectionSort(Comparable[] a)
{
int N = a.length;
0 1 2 3 4 5 6 7 8 9 10 11
for (int i = 0; i < N‐1; i++)
{
int min = i;
for (int j = i+1; j < N; j++) 2 16 4 8 12 6 4 10 16 8 18 14
{
if (less(a[j], a[min])) 0 1 2 3 4 5 6 7 8 9 10 11
min = j;
}
swap(a, i, min); 2 4 16 8 12 6 4 10 16 8 18 14
}
} 0 1 2 3 4 5 6 7 8 9 10 11

26 6 15 13 14 1 2 4 4 8 12 6 16 10 16 8 18 14
0 1 2 3 4 5 6 7 8 9 10 11

2 4 4 6 12 8 16 10 16 8 18 14
0 1 2 3 4 5 6 7 8 9 10 11

In final position, in No element in this region is less


non-decreasing than any of the first k elements.
order.

2 4 4 6 8 8 10 12 14 16 16 18
0 1 2 3 4 5 6 7 8 9 10 11

2
1/31/2024

Analysis of selection sort


public void selectionSort(Comparable[] a)
{ Selection sort is O(N2).
Note that for any array A[1..N], the portion
int N = a.length;
Standard loop analysis
for (int i = 0; i < N‐1; i++) 
Both less() and swap() are O(1)
A[1..1] consisting of the single entry A[1] is
{
int min = i;
for (int j = i+1; j < N; j++)
{ already sorted.
if (less(a[j], a[min]))
min = j;
~N2/2 comparisons  Insertion sort works by extending the
}
swap(a, i, min); ~N swaps
length of the sorted portion one step at a
}
} time:
◦ After zero iterations, A[1] is sorted
◦ After one iteration, A[1..2] is sorted
Data movement is minimal.
◦ After two iterations, A[1..3] is sorted
After three iterations A[1..4] is sorted, and so on,
Only a linear number of swaps will be made.

Running time is not sensitive to input. until A[1..N] is sorted.
Running time will be quadratic if the array is already sorted, almost sorted, or in
reverse order.

10

Insertion sort
Walk from left to right through the array.
On each step, insert the element in the
current location in sorted order to its left.

After k steps, the first k + 1 elements are


in sorted order relative to each other

k=1 k=2 k=3 k=4

k=5 k=6 k=N-1

... Figure 7.1: An example illustrating what Insertion Sort does when i=6 and
j=2 (top). The array before inserting, and (bottom) the insertion gap.

3
1/31/2024

Insertion Sort
Insertion sort (Java version)
8 16 4 8 12 6 4 10 16 2 18 14
public void insertionSort(Comparable[] a)
{
int N = a.length;
0 1 2 3 4 5 6 7 8 9 10 11
for (int i = 1; i < N; i++) {
int j = i;
while ((j > 0) &&
(less(a[j], a[j‐1]))) { 8 16 4 8 12 6 4 10 16 2 18 14
swap(a, j, j‐1);
j‐‐; 0 1 2 3 4 5 6 7 8 9 10 11
}
}
} 4 8 16 8 12 6 4 10 16 2 18 14
0 1 2 3 4 5 6 7 8 9 10 11

26 6 15 13 14 1 4 8 8 16 12 6 4 10 16 2 18 14
Invariants
0 1 2 3 4 5 6 7 8 9 10 11
On the kth iteration of the i loop:

The first k+1 elements are 4 8 8 12 16 6 4 10 16 2 18 14


in sorted order relative to 0 1 2 3 4 5 6 7 8 9 10 11
each other

Elements k+2 through the In non-decreasing In original order; not processed.


last element are in their order.
original positions and have
not been accessed
2 4 4 6 8 8 10 12 14 16 16 18
0 1 2 3 4 5 6 7 8 9 10 11

Analysis of insertion sort


public void insertionSort(Comparable[] a)
{ Insertion sort is O(N2).
int N = a.length;
Standard loop analysis
for (int i = 1; i < N; i++) {
int j = i; Both less() and swap() are O(1)
For a list of length n, selection sort makes about n2
while ((j > 0) &&
(less(a[j], a[j‐1]))) { ~N2/2 comparisons in the worst case 
swap(a, j, j‐1);
j‐‐;
~N2/2 swaps in the worst case key comparisons and exactly n-1 swap operations
}

}
}  Therefore, it is generally better than the other
quadratic sorts if the elements are in random or
Running time is sensitive to input.
reverse order (not affected by the original order of
the array elements)
Best case: If the array is in ascending order (already sorted), insertion sort
makes N-1 comparisons and 0 swaps. If the array is nearly sorted, insertion sort  The insertion sort is particularly appropriate for
makes a linear number of comparisons and swaps.
small arrays that are nearly sorted
Worst case: If the array is in descending order, insertion sort makes ~N2/2
comparisons and ~N2/2 swaps.

Average case: If the array is in random order, insertion sort makes ~N2/4
comparisons and ~N2/4 swaps on average.

4
1/31/2024

You might also like