0% found this document useful (0 votes)
54 views10 pages

Array Operation and Sorting

The document summarizes array operations and sorting algorithms. It describes flipping an array, calculating the sum of an array, bubble sort, and insertion sort. Bubble sort loops through the array multiple times to compare and swap adjacent elements. Insertion sort iterates through the array and inserts each element into the sorted position by shifting greater elements over.

Uploaded by

Hassan Javed
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)
54 views10 pages

Array Operation and Sorting

The document summarizes array operations and sorting algorithms. It describes flipping an array, calculating the sum of an array, bubble sort, and insertion sort. Bubble sort loops through the array multiple times to compare and swap adjacent elements. Insertion sort iterates through the array and inserts each element into the sorted position by shifting greater elements over.

Uploaded by

Hassan Javed
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/ 10

Lecture 5-2

Array operations & sorting


Eng. Shahid Ismail
Algo & DataStructures
Operations of numerical arrays
1. Flip
2. Summation
Sorting (Bubble sort , Insertion Sort)

2
start
Algo & DataStructures
• Algorithm FLIP(A)
Take size of
• Input: Array to be flipped. Array (sz)
• Output: Flipped array.
• Data structures: An array A[L … U], B[L….U]

sz==0
1. sz=SIZE_ARRAY(A)
2. If (sz==0) then Empty Array
1. Print “ Empty vector”
2. Exit
3. Else
4. i = 0;
B[i] = A[sz-i] i < sz
1. While i < sz do
1. B[i] = A[sz-i])
2. i=i+1
2. End While Print flipped
5. End if i=i+1
Array
6. Print(‘Sorting Success)
7. Print Sorted Array
stop
start
Algo & DataStructures
• Flow chart
Take size of
• Algorithm summation(A) Array (sz)
• Input: Array to be summed.
• Output: Sum.
• Data structures: An array A[L … U]
sz==0

1. sz=SIZE_ARRAY(A)
Empty Array
2. If (sz==0) then
1. Print “ Empty vector”
2. Exit Sum = Sum +
i < sz
3. Else A[i]
4. i = 0; Sum=0;
1. While i < sz do
1. Sum = Sum+A[i])
2. i=i+1 i=i+1 sum
2. End While
5. End if
6. Print(‘Sum)
stop
Sorting
Sorting is any process of arranging items systematically, and has two common,
yet distinct meanings:
1. ordering: arranging items in a sequence ordered by some criterion;
2. categorizing: grouping items with similar properties.

• Bubble sort
• Insertion Sort
Sorting Bubble sort
General Algorithm
Loop 1: For i = 0 to N-1 // Loop1
Loop 2: For j = i + 1 to N – I // Loops 2
Comparison : if A[j] > A[i] //compare
Swap A[J] and A[i]
End Loop
End Loop
Exit
Sorting Bubble sort- Illustration
Sorting Insertion sort

Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its
predecessor.
3: If the key element is smaller than its predecessor,
compare it to the elements before. Move the greater
elements one position up to make space for the
swapped element.

You might also like