0% found this document useful (0 votes)
11 views28 pages

Array Data Structures and Sorting Methods

This document provides an overview of linear data structures, specifically arrays, including their representation, operations, and applications such as sorting and searching. It details various sorting algorithms like Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort, and Radix Sort, along with their complexities and characteristics. Additionally, it discusses array operations, time complexities, and sorting terminology, including concepts like in-place sorting and stable sorting.

Uploaded by

gaikwadvedant03
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)
11 views28 pages

Array Data Structures and Sorting Methods

This document provides an overview of linear data structures, specifically arrays, including their representation, operations, and applications such as sorting and searching. It details various sorting algorithms like Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, Merge Sort, and Radix Sort, along with their complexities and characteristics. Additionally, it discusses array operations, time complexities, and sorting terminology, including concepts like in-place sorting and stable sorting.

Uploaded by

gaikwadvedant03
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

UNIT II

Linear Data Structure - Array


UNIT II
2.1 Array as ADT
2.2 Representation
2.3 Applications
2.3.1 Sorting: Concept, terminology, types. Methods:
a) Bubble Sort,
b) Insertion Sort,
c) Selection sort
d) Quick Sort
e) Merge Sort
f) Radix sort.
Comparison of sorting methods.
2.3.2 Searching: Linear, Binary
Array Representation
1. Array Index: Array index starts from 0.

0 1 2 3 4
10 20 30 40 50

2. Array element: Items stored in an array


3. Array Length : Array size

Types of Arrays :
One dimensional array
Multi-dimensional array
Array Operation

1. Traverse − Print all the elements in the array one


by one.
2. Insertion − Adds an element at the given index.
3. Deletion − Deletes an element at the given index.
4. Search − Searches an element in the array using
the given index or the value.
5. Update − Updates an element at the given index.
Time complexity of Array

Operation Average Case Worst Case


Read O(1) O(1)
Insert O(n) O(n)
Delete O(n) O(n)
Search O(n) O(n)
Applications of Arrays
1. Sorting
 Rearrangement of data
 According to some criteria
 Orders : Ascending ,Descending
Sorting Terminology
1. In-place sorting
i. The algorithm doesn't use extra space
for sorting.
ii. Sorting is applied to the data set
within its own storage and do not
produce extra arrays or lists.
iii. EX: reverse array
reverse_array(array A[n])
[Link]
for i from 0 to n/2
temp = A[i]
A[i] = A[n - 1 - i]
A[n - 1 - i] = temp
No extra arrays are used . Output is stored in
the same array.
Sorting Terminology
2. Internal and External Sorting
When all data is placed in-memory, then sorting
is called internal sorting.
When all data sorted needs extra memory, then
sorting is called External sorting.
External storage : hard-disk, CD,PD etc.
Sorting Terminology
3. Stable sorting
A sorting algorithm is said to be stable if two objects with
equal keys appear in the same order in sorted output as
they appear in the input array to be sorted.
Ex.: I/P
O/P
10 20 20 30 10

10 10 20 20 30

Bubble Sort, Insertion Sort, Merge Sort, Count Sort etc.


Bubble Sort

[Link]
Bubble Sort algorithm
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i],
list[i+1])
end if
end for
return list e
end BubbleSort
Bubble Sort algorithm
14 33 27 35 10 14 27 10 33 35

ITERATION 2

14 33 27 35 10 14 10 27 33 35

ITERATION 3
SWAP
14 27 33 35 10 10 14 27 33 35

ITERATION 4
14 27 33 35 10
Complexity = O(n2)
SWAP
Worst Case Complexity:O(n2)
14 27 33 10 35
Best Case Complexity:O(n)
ITERATION 1
Selection Sort
• The selection sort algorithm sorts an array by
repeatedly finding the minimum element from
unsorted part and putting it at the beginning.
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
Selection Sort
• Array[0..4] Array [3..4]
64 25 12 22 11 11 12 22 25 64

11 25 12 22 64
11 12 22 25 64

• Array[1..4]
11 25 12 22 64
Complexity = O(n2)

11 12 25 22 64

• Array[2..4]

11 12 25 22 64

11 12 22 25 64
Insertion Sort Algorithm

insertionSort(array)
mark first element as sorted
for each unsorted element X
'extract' the element X
for j <- lastSortedIndex down to 0
if current element j > X
move sorted element to the right by
1
break loop and insert X here
end insertionSort
Merge Sort(divide n Conquer)
10 40 20 30 60 80 70 10
Algorithm Merge(array A, m,mid,n)
{ i=1;j=1;k=1;
while(i<=lb&&j<=ub)
{ if(A[i]<B[j])
c[k++]=A[i++];
else
c[k++]=B[j++];
}
for(;i<=lb;i++)
c[k++]=A[i];
for(;j<=ub;j++)
c[k++]=B[j];
}
*Recursive MergeSort:
Algorithm MergeSort(A[],lb,ub)

10 40 20 30 60 80 70 10 0..7 mid -3
if(lb<ub)
{ 10 40 20 30 0..3 mid -1 60 80 70 10

mid=(lb+ub)/2 10 40 20 30

MergeSort(A,lb,mid)
MergeSort(A,mid+1,ub)
Merge(A[] ,lb,mid,ub)

}
MS(0,8)

MS(0,4) MS(5,8) Merge(0,4,8)

MS(0,2) MS(3,4) Merge(0,2,4) MS(5,6) MS(7,8) Merge(5,6,8)

MS(0,1) MS(2,2) Merge(0,1,2) MS(5,5) MS(6,6) Merge(5,5,6)

MS(3,3) MS(4,4) Merge(3,3,4)

MS(0,0) MS(1,1) Merge(0,0,1) MS(7,7) MS(8,8) Merge(7,8,8)


9 3 7 5 6 4 8 2

9 3 7 5 6 4 8 2

9 3 7 5 6 4 8 2

9 3 7 5

3 9

2 3 4 5 6 7 8 9
Quick Sort
Void QuickSort(Array a,lb,ub)
{
if (lb<ub)
j= partition(a,lb,ub)
QuickSort(a,lb,j-1)
QuickSort(a,j+1,ub)
}
Quick sort algorithm
partition
1. Start
2. dn=lb up=ub
3. pivot =a[lb]
4. while(a[dn]<= pivot && dn<ub) dn++
5. while(a[up]> pivot && up>lb) up—
6. If(dn<up) swap a[dn],a[up] goto step 4
7. Swap a[up],a[lb]
8. Return up
9. Stop

* If up and down cross each other then swap a[up],pivot


Quick Sort
lb ub Action

23(pivot) 35 8 10 26 29 20 15 A[dn]>pivot
dn up A[up]<pivot
Swap a[dn],a[up]

23(pivot) 15dn 8 10 26 29 20 35up

23(pivot) 15 8dn 10 26 29 20 35up

23(pivot) 15 8 10dn 26 29 20 35up

23(pivot) 15 8 10dn 26 29 20up 35 Swap a[dn],a[up]

23(pivot) 15 8 20dn 26 29 10up 35

23(pivot) 15 8 20 26dn 29 10up 35 Swap a[dn],a[up]

23(pivot) 15 8 20 10dn 29 26up 35

23(pivot) 15 8 20 10 29dn,up 26 35

23(pivot) 15 8 20 10 up 29dn 26 35 up crosses dn


swap
(pivot,a[up])
10 15 8 20 23 29 26 35

Partition1 Partition2
55pivot 7dn 80 32 18 23 82 62up Action
55pivot 7 80dn 32 18 23 82 62up
55pivot 7 80dn 32 18 23 82up 62
55pivot 7 80dn 32 18 23up 82 62
55pivot 7 23dn 32 18 80up 82 62
55pivot 7 23 32dn 18 80up 82 62
55pivot 7 23 32 18dn 80up 82 62
55pivot 7 23 32 18 80up, 82 62
dn
55pivot 7 23 32 18up 80 dn 82 62
18 7 23 32 55up 80 dn 82 62
18pivot 7up 23dn, 32
7 18 23 32
25 15 5 60 10 45 Action
Radix sort
[121, 432, 564, 23, 1, 45,
788]

121 001 001

001 121 023

432 023 045

023 432 121

564 045 432

045 564 564

788 788 788


Comparison

You might also like