KEMBAR78
Programming Fundamentals lecture-14.pptx
1
Programming Fundamentals
LECTURE-14
GHULAM ABBAS
abbas.sbcws@gmail.com
2
In the Previous Lecture
Arrays
Types of array
One-dimensional arrays
Declaration of array
Initializing an array
Copying arrays
3
Today’s lecture
Swapping
 function and Array
Sorting of an array
Sorting algorithms
Insertion sort
Bubble sort
Selection sort
4
Example: Swapping
int num [ ] ={12,4,6,7,78};
for(int i=0; i<=4;i++)
{
cout<<num[i]<<"t";
}
cout<<endl;
int x ;
x = num [ 0 ] ;
num [ 0 ] = num [ 3 ] ;
num [ 3 ] = x ;
for(int i=0; i<=4;i++)
{
cout<<num[i]<<"t";
}
}
5
Example:function and Array
void functionAndArray(int[], int);
main()
{
int const size=5;
int fArray[]={12,5,14,56,73,34};
functionAndArray(fArray,size);
}
void functionAndArray(int fArray[],int size)
{
for(int i=0;i<size;i++)
{
cout<<fArray[i]<<"t";
}
}
6
Example:function and Array
void functionArray ( int [ ] , int ) ;
main ( )
{
int numbers [ 100 ] ;
functionArray ( numbers , 100) ;
}
void functionArray( int x [ ] , int arraySize )
{
int i ;
for ( i = 0 ; i < arraySize ; i ++)
{
x [ i ] = i ;
cout<<x[i]<<"t";
}
}
7
Sorting
int sort[]={23,4,66,2,44,77,43};
for(int i=0;i<7;i++)
{
for(int j=i+1;j<7;j++)
{
if(sort[j]<sort[i])
{
int temp=sort[i];
sort[i]=sort[j];
sort[j]=temp;
}
}
cout<<sort[i]<<"t";
}
}
O
utput
8
Sorting Algorithms
Insertion sort
 Like sorting a hand of playing cards start with an empty hand
and the cards facing down the table.
 Pick one card at a time from the table, and insert it into the
correct position in the left hand.
 Compare it with each of the cards already in the hand, from
right to left
 The cards held in the left hand are sorted.
10 Insertion Sort
5 2 4 6 1 3
input array
left sub-array right sub-array
at each iteration, the array is divided in two sub-arrays:
sorted unsorted
11 Insertion Sort
code
for(i=1;i<count;i++)
{
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0))
{
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}
cout<<“Order of Sorted
elements: “;
for(i=0;i<count;i++)
cout<<number[i];
Bubble sort
 In bubble sort, each element is compared with its
adjacent element.
 We begin with the 0th element and compare it with the
1st element.
 If it is found to be greater than the 1st element, then
they are interchanged.
 In this way all the elements are compared (excluding
last) with their next element and are interchanged if
required
 On completing the first iteration, largest element gets
placed at the last position.
 Similarly in second iteration second largest element gets
placed at the second last position and so on.
14 Example
1
3
2
9
6
4
8
i = 1 j
3
1
2
9
6
4
8
i = 1 j
3
2
1
9
6
4
8
i = 1 j
3
2
9
1
6
4
8
i = 1 j
3
2
9
6
1
4
8
i = 1 j
3
2
9
6
4
1
8
i = 1 j
3
2
9
6
4
8
1
i = 1 j
3
2
9
6
4
8
1
i = 2 j
3
9
6
4
8
2
1
i = 3 j
9
6
4
8
3
2
1
i = 4 j
9
6
8
4
3
2
1
i = 5 j
9
8
6
4
3
2
1
i = 6 j
9
8
6
4
3
2
1
i = 7
j
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout <<"Sorted Element List ...n";
for(i = 0; i<10; i++)
{
cout
<<a[i]<<"t";
}
Code
Selection sort
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it
with the element in the second position
Continue until the array is sorted
17 Example
1
3
2
9
6
4
8
8
3
2
9
6
4
1
8
3
4
9
6
2
1
8
6
4
9
3
2
1
8
9
6
4
3
2
1
8
6
9
4
3
2
1
9
8
6
4
3
2
1
9
8
6
4
3
2
1
code
int a[100], n, i, j, swap;
cout<<"Enter number of elementsn";
cin>>n;
cout<<"Enter
Numbers"<<n<<endl;
for (i = 0; i < n; i++)
cin>>a[i];
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
if(a[i] > a[j])
{
swap=a[i];
a[i]=a[j];
a[j]=swap;
}
}
}
cout<<"Sorted Array";
for(i = 0; i < n; i++)
cout<<"t"<<a[i]<<"t";
return 0;
}

Programming Fundamentals lecture-14.pptx