Lect2 Array Part2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

ARRAYS

https://cse.iitkgp.ac.in/pds/semester/2006a/Array.ppt
How is an array stored?
 Starting from a given memory location, the
successive array elements are allocated
space in consecutive memory locations.

Array a
 x: starting address of the array in memory
 k: number of bytes allocated per array element
 a[i]  is allocated memory location at
address x + i*k
Index Rule
 An array index must evaluate to an int
between 0 and n-1 where n is the number of
elements in the array.
 marks[76]

C Array bounds are not checked


#define S 100 if (0<=i && i<100)
marks[S] = 10; marks[i] = 10;
else printf (“Array index %d
out of range”, i);
4

Relationship Between Pointers and Arrays


• 5 element int array on a machine using 4 byte ints
– By statement: vPtr = &v[0]; vPtr points to first
element v[ 0 ], which is at location 3000 i.e. vPtr = 3000
– vPtr += 2; sets vPtr to 3008
vPtr points to v[ 2 ]

location

3000 3004 3008 3012 3016

v[0] v[1] v[2] v[3] v[4]

pointer variable vPtr

 2003 Prentice Hall, Inc. All rights reserved. https://www.cyut.edu.tw/~ywfan/C++/cpphtp4_PPT_05.ppt


5

Relationship Between Pointers and Arrays

• Arrays and pointers closely related


– Array name like constant pointer
– Pointers can do array subscripting operations
• Accessing array elements with pointers
– Element b[ n ] can be accessed by *( bPtr + n )
• Called pointer/offset notation
– Addresses
• &b[ 3 ] same as bPtr + 3
– Array name can be treated as pointer
• b[ 3 ] same as *( b + 3 )
– Pointers can be subscripted (pointer/subscript notation)
• bPtr[ 3 ] same as b[ 3 ]

 2003 Prentice Hall, Inc. All rights reserved. https://www.cyut.edu.tw/~ywfan/C++/cpphtp4_PPT_05.ppt


Example
int a[10]; *p = 1  a[0] = 1
The following are
equivalent (p+1)  &a[1]

int *p; (p+i)  & a[i]


p = &a[0];
*(p+i)  a[i]
int *p = &a[0];

int *p = a ;
https://bohr.wlu.ca/cp264/notes/cp264_lecture07_array.ppt
Array operations
int readarray (int x[], int size) {
#define MAXS 100 int i;
int insert (int[], int, int, int) ; for (i=0; i<size; i++)
int reverse (int[], int) ; scanf(“%d”, &x[i]) ;
int getelement (int[], int, int) ; return size;
int readarray (int[], int) ; }
main () { int getelement (int x[], int size, int pos){
int a[MAXS]; if (pos <size) return x[pos] ;
int size; return -1;
size = readarray (a, 10) ; }
size = insert (a, size, 4, 7) ;
int insert (int x[], int size, int pos, int val){
x = getelement (a, size, 3) ;
for (k=size; k>pos; k--)
size = reverse (a, size) ;
x[k] = x[k-1] ;
} x[pos] = val ;
return size+1;
}
void reverse (int x[], int size) {
int i;
for (i=0; i< (size/2); i++)
temp = x[size-i-1] ;
x[size-1-1] = x[i] ;
x[i] = temp;
}
Selection Sort Algorithm
1. for fill = 0 to n-2 do // steps 2-6 form a pass
2. set posMin to fill
3. for next = fill+1 to n-1 do
4. if item at next < item at posMin
5. set posMin to next
6. Exchange item at posMin with one at fill

Chapter 10: Sorting 9


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Selection Sort Example
35 65 30 60 20 scan 0-4, smallest 20
swap 35 and 20
20 65 30 60 35 scan 1-4, smallest 30
swap 65 and 30
20 30 65 60 35 scan 2-4, smallest 35
swap 65 and 35
20 30 35 60 65 scan 3-4, smallest 60
swap 60 and 60
20 30 35 60 65 done

Chapter 10: Sorting 10


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Bubble Sort

• Compares adjacent array elements


• Exchanges their values if they are out of order

• Smaller values bubble up to the top of the array


• Larger values sink to the bottom

Chapter 10: Sorting 11


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Bubble Sort Example

Chapter 10: Sorting 12


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Bubble Sort Algorithm
1. do
2. for each pair of adjacent array elements
3. if values are out of order
4. Exchange the values
5. while the array is not sorted

Chapter 10: Sorting 13


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Bubble Sort Algorithm, Refined
1. do
2. Initialize exchanges to false
3. for each pair of adjacent array elements
4. if values are out of order
5. Exchange the values
6. Set exchanges to true
7. while exchanges

Chapter 10: Sorting 14


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Analysis of Bubble Sort
• Excellent performance in some cases
• But very poor performance in others!
• Works best when array is nearly sorted to begin with
• Worst case number of comparisons: O(n2)
• Worst case number of exchanges: O(n2)
• Best case occurs when the array is already sorted:
• O(n) comparisons
• O(1) exchanges (none actually)

Chapter 10: Sorting 15


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Insertion Sort Algorithm
• For each element from 2nd (nextPos = 1) to last:
• Insert element at nextPos where it belongs
• Increases sorted subarray size by 1

• To make room:
• Hold nextPos value in a variable
• Shuffle elements to the right until gap at right place

Chapter 10: Sorting 16


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Visual Example

Jordan Mash, CS32, Bryce Boe


Analysis of Insertion Sort

• Maximum number of comparisons: O(n2)


• In the best case, number of comparisons: O(n)
• # shifts for an insertion = # comparisons - 1
• When new value smallest so far, # comparisons
• A shift in insertion sort moves only one item
• Bubble or selection sort exchange: 3 assignments

Chapter 10: Sorting 18


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Best Times to Use Insertion Sort

• When the data sets are relatively small.


• Moderately efficient.
• When you want a quick easy implementation.
• Not hard to code Insertion sort.
• When data sets are mostly sorted already.
• (1,2,4,6,3,2)

Jordan Mash, CS32, Bryce Boe


Insertion Sort Works in Place
• No extra data structures needed. It works off of
original data structure that it is fed with and simply
swaps the position of the items in the set.
• It does not require any extra memory as data sets get
larger. Will always require the same amount of
memory.

Jordan Mash, CS32, Bryce Boe


Comparison of Quadratic Sorts
• None good for large arrays!

Chapter 10: Sorting 21


https://people.cs.umass.edu/~moss/187/lectures/lecture-k-
Binary Search Example

7 12 42 59 71 86 104 212

Looking for 89

https://www.cc.gatech.edu/~bleahy/cs1311/cs1311lecture24wdl.ppt
Binary Search Example

7 12 42 59 71 86 104 212

Looking for 89

https://www.cc.gatech.edu/~bleahy/cs1311/cs1311lecture24wdl.ppt
Binary Search Example

7 12 42 59 71 86 104 212

Looking for 89

https://www.cc.gatech.edu/~bleahy/cs1311/cs1311lecture24wdl.ppt
Binary Search Example

7 12 42 59 71 86 104 212

89 not found – 3 comparisons

3 = Log(8)

https://www.cc.gatech.edu/~bleahy/cs1311/cs1311lecture24wdl.ppt
Binary Search Big-O

• An element can be found by comparing


and cutting the work in half.
– We cut work in ½ each time
– How many times can we cut in half?
– Log2N

• Thus binary search is O(Log N).

https://www.cc.gatech.edu/~bleahy/cs1311/cs1311lecture24wdl.ppt

You might also like