Array Operation and Sorting
Array Operation and Sorting
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.