You are on page 1of 24

,

-13

: . //

.
.
www.emust.edu.mn


. ,
.
: 1 , 2 , n : 1 ,
2 , n .
.

.
.


.
.
( ) .


.
.
( )
( ) .


.
.
, ,

.
.

.


.
.

. N N2
N .

. ( )
N
.



.
,

,
.
selectionSort ()
.

template<class Type> void selectionSort(Type a[], int n) (
int i, j, min;
for (i=0; i<n-1; i++) ( min = i;
for ( j = i+1 ; j<n ; j++ )
if ( a [ j ] < a [ min ] ) min = j ;
swap ( a , I , min ) ;
Selection ( ) . i
. J
I
swap ()
. i
.

selectionSort ( )

. int char
. int a [ 8 ] = { 77, 33, 44, 11,
88, 22, 66, 55 } ;
char s [ 15 ] = {a , b, o , r, t, n ,
g, e, x, a, m, p, l, e} ;
selectionSort ( a, 8) ;
selectionSort ( s, 15) ;

.


n2 , n .
i n-1
.
( n )
( n ) = (n-1) + (n-2) +.+ 2 + 1 = n(n-1)/2 = O (n2)
.
NM N2
NM . N=O(M)
.



, .


.

.
InsertionSort ( ) .

template<class Type>
void insertitionSort (Type a [ ] , int I , int n ) {
for (int I = 1 ; I < n; I ++) {
Type element = a [ I ] ;
Int j = I 1 ;
While ( j >= 0 && element < a [ j ] )
a [ j+1 ] = a [ j] ;
a [ j+1 ] = element ;
}
}

I
.

. ,

.
while

.


( )
.
j ( )
. while j>=0
. j

.
( )
.

.

while ( element <a[ j ] ) .


. f ( n )
.
.

k
k-1 . f(n) = 1 + 2 + 3 + + (n-
1) / 2 O(n2)

.
.
(k-1) /2

f(n) = + 2/2 + 3/2 + + (n-1) /2 = n(n-1) / 4 = O(n2)


.


. ,
.
: 1 , 2 , n : 1 ,
2 , n .
.

.
.


.
1- 2 . N
.



.

.

.
.
bubbleSort ( ) .

Template<class Type>void bubbleSort (Type a [ ] , int n) {for ( int i=n-1; i>0 ;
I--)
for ( int j=0; j<i ; j ++)
if (a[ j ] > a [ j+1 ] ) swap (a, j , j+1) ;
}
i

. j
i .

i-
.

.


f(n) = (n-1) + (n-2) + . + 2 + 1 = (n-1)/2 = O(n2)
.
. O(n2)
.


.
.
Template<class Type>
Void bubbleSort (Type a [] , iny n ) {
bool sorted = false ;
for (int i=n-1; I >o && !sorted ; I ++ ) {
sorted = true ;
for (int j = 0 ; j <1 ; j ++)
if ( a [j] > a [ j+1 ] ) {
swap ( a, j, j+1 ) ;
sorted = false ;

sorted
.


.
(n-1) + (n-2) + .+ (n-ksorted ) * (2n ksorted -1 ) /2

ksorted
.
!sorted .
ksorted
.
1*(2n-2)/2 = n-1 = O(n)
. ksorted n-
1
(n-1)n/2 = O/n2 .

1. U.CS101 13
2. http://flowgorithm.org/documentation flowgorithm

You might also like

  • Lecture 15
    Lecture 15
    Document40 pages
    Lecture 15
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lec1-1 Corporate Activity Shine
    Lec1-1 Corporate Activity Shine
    Document61 pages
    Lec1-1 Corporate Activity Shine
    Amarsaikhan Tuvshinbayar
    0% (1)
  • Lecture 13
    Lecture 13
    Document46 pages
    Lecture 13
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture - 10
    Lecture - 10
    Document35 pages
    Lecture - 10
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 9
    Lecture 9
    Document46 pages
    Lecture 9
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 8 2018
    Lecture 8 2018
    Document54 pages
    Lecture 8 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 12
    Lecture 12
    Document34 pages
    Lecture 12
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 5 2018
    Lecture 5 2018
    Document54 pages
    Lecture 5 2018
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 3 2018
    Lecture 3 2018
    Document42 pages
    Lecture 3 2018
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 2 2018
    Lecture 2 2018
    Document33 pages
    Lecture 2 2018
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Lecture 11
    Lecture 11
    Document24 pages
    Lecture 11
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 6 2018
    Lecture 6 2018
    Document50 pages
    Lecture 6 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 4 2018
    Lecture 4 2018
    Document47 pages
    Lecture 4 2018
    Amarsaikhan Tuvshinbayar
    100% (4)
  • Lecture 7 20108
    Lecture 7 20108
    Document47 pages
    Lecture 7 20108
    Amarsaikhan Tuvshinbayar
    75% (4)
  • IT101 Lecture 4
    IT101 Lecture 4
    Document72 pages
    IT101 Lecture 4
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 1 2018
    Lecture 1 2018
    Document39 pages
    Lecture 1 2018
    Amarsaikhan Tuvshinbayar
    100% (6)
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 4
    Programming Lec 4
    Document39 pages
    Programming Lec 4
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 9
    Programming Lec 9
    Document39 pages
    Programming Lec 9
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 7
    Programming Lec 7
    Document57 pages
    Programming Lec 7
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 2
    Lecture 2
    Document80 pages
    Lecture 2
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 1
    Lecture 1
    Document54 pages
    Lecture 1
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Programming Lec 8
    Programming Lec 8
    Document32 pages
    Programming Lec 8
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • PL - 3
    PL - 3
    Document47 pages
    PL - 3
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Koosen 2018 Algorithm Programming 3
    Koosen 2018 Algorithm Programming 3
    Document46 pages
    Koosen 2018 Algorithm Programming 3
    Amarsaikhan Tuvshinbayar
    100% (1)