0% found this document useful (0 votes)
41 views13 pages

DS

data structures

Uploaded by

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

DS

data structures

Uploaded by

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

INTERNAL SORTING:

The objective is to take an unordered set of comparable data


items and arrange them in
order. We will usually sort the data into ascending order sorting into
descending order is very
similar.
Data can be sorted in various ADTs, such as arrays and trees
In computer science, a sorting algorithm is an algorithm that puts
elements of a list in a
certain order.
The most-used orders are numerical order and lexicographical
order. Efficient
sorting is important for optimizing the use of other algorithms (such as
search and merge
algorithms) that require sorted lists to work correctly;
It is also often useful for producing human-readable output. More
formally, the output must satisfy two conditions:

1. The output is in non decreasing order (each element is no


smaller than the previous element according to the desired total order);

2. The output is a permutation, or reordering, of the input.


Insertion sort:
Insertion sort is a simple sorting algorithm that is relatively
efficient for small lists and mostly sorted lists, and often is used as part
of more sophisticated algorithms.

It works by taking elements from the list one by one and


inserting them in their correct position into a new sorted
list.
In arrays, the new list and the remaining elements can share the
array's space, but insertion is expensive, requiring shifting all following
elements over by one.
Insertion sort is a simple sorting algorithm: a comparison sort in
which the sorted array (or list) is built one entry at a time. It is much less
efficient on large lists than more advanced algorithms such as quicksort,
heapsort, or merge sort. However, insertion sort provides several

Advantages:

 Simple implementation
 Efficient for (quite) small data sets
 Adaptive (i.e., efficient) for data sets that are already substantially
sorted: the time complexity is O(n + d), where d is the number of
inversions
 More efficient in practice than most other simple quadratic (i.e.,
O(n2)) algorithms such as selection sort or bubble sort; the best case
(nearly sorted input) is O(n)
Stable ; i.e., does not change the relative order of elements with equal
keys
In-place ; i.e., only requires a constant amount O(1) of additional
memory space
Online ; i.e., can sort a list as it receives it
Insertion Sort Algorithm
Now we have a bigger picture of how this sorting technique
works, so we can derive simple steps by which we can achieve insertion
sort.

Step 1 − If it is the first element, it is already sorted. return 1;

Step 2 − Pick next element

Step 3 − Compare with all elements in the sorted sub-list

Step 4 − Shift all the elements in the sorted sub-list that is greater than
the value to be sorted

Step 5 − Insert the value

Step 6 − Repeat until list is sorted


Algorithm:

Insertion-Sort(A) for j = 2 to A.length


key = A[j]
i=j–1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key
Take array A[]=[7,4,5,2].

Take array A[]=[7,4,5,2].


QUICK SORT

Quicksort is a divide and conquer algorithm which relies on a


partition operation: to partition an array an element called a pivot is
selected.

All elements smaller than the pivot are moved before it and all
greater elements are moved after it. This can be done efficiently in
linear
time and in-place.

The lesser and greater sublists are then recursively sorted.


Efficient implementations of quicksort (with in-place partitioning) are
typically unstable sorts and somewhat complex, but are among the
fastest sorting algorithms in practice.
The most complex issue in quicksort is choosing a good pivot
element;

consistently poor choices of pivots can result in drastically


slower O(n²) performance, if at each step the median is chosen as the
pivot then the algorithm works in O(n log n).

Finding the median however, is an is an O(n) operation on


unsorted lists and therefore exacts its own penalty with sorting.

Quicksort is a divide and conquer algorithm. Quicksort first


divides a large list into two smaller sub-lists: the low elements and the
high elements.
Quicksort can then recursively sort the sub-lists.

The steps are:

1. Pick an element, called a pivot, from the list.

2. Reorder the list so that all elements with values less than the pivot
come before the pivot, while all elements with values greater than the
pivot come after it (equal values can go either way).
After this partitioning, the pivot is in its final position. This is called the
partition operation.

3. Recursively sort the sub-list of lesser elements and the sub-list of


greater elements.
The base case of the recursion are lists of size zero or one, which never
need to be sorted. simple pseudocode

function quicksort(array)
var list less, greater
if length(array) ≤ 1
return array // an array of zero or one elements is already sorted
select and remove a pivot value pivot from array
for each x in array
if x ≤ pivot then append x to less
else append x to greater
return concatenate(quicksort(less), pivot, quicksort(greater)

You might also like