You are on page 1of 4

DSA Assignment 2

Muhammad Sufian
Enrollment Number: 01-134132-130
Class: BS(CS), 4B
Course: Data Structures and Algorithms
sunnykhan.msk@gmail.com
May 1, 2015

The Steps Taken to Write the Program

My program is doing some sorting functions using doubly link lists. sorting
algorithems includes the following 1- insertion sort 2- selection sort 3- bubble
sort
1- INSERTION SORT: it picks a number and compare that number with
others if coindition become true it swaps that number with the smallest number.
2- SELECTION SORT: it selects the number and compares that number with
the rest and swapswhere the coindition gets true. 3- BUBBLE SORT: it compare
two numbers and swaps them if coindition gets true.

The Difficulties Faced

INSERTION SORT WAS DIFFICULT FOR ME. SO I TOOK SOME HELP


FROM MY FRIENDS.
.

3
1

11

The Code Section

# i n c l u d e <i o s t r e a m >
# i n c l u d e <c o n i o . h>
u s i n g namespace s t d ;
s t r u c t node
{
i n t data ;
node next , p r e v ;
};
class sortings
{

13

15

17

19

21

23

25

27

29

31

33

35

37

39

41

43

node head , t a i l ;
public :
sortings ()
{
head = NULL;
t a i l = NULL;
}
void i n s e r t ( )
{
node temp ;
temp = new node ;
c o u t << ENTER DATA : ;
c i n >> temp>data ;
temp>n e x t = NULL;
temp>p r e v = NULL;
i f ( head == NULL)
{
head = temp ;
t a i l = temp ;
}
else
{
t a i l >n e x t = temp ;
t a i l = temp ;
}
}
void display ( )
{
node temp = head ;
w h i l e ( temp != NULL)
{
c o u t << temp>data << ;
temp = temp>n e x t ;

45

47

49

51

53

55

57

59

61

63

65

67

}
}
void s e l e c t i o n ( )
{
node temp ;
f o r ( node temp1 = head ; temp1 != NULL; temp1 = temp1>n e x t )
{
f o r ( node temp = head ; temp != NULL; temp = temp>n e x t )
{
i f ( temp1>data < temp>data )
{
i n t temp2 ;
temp2 = temp1>data ;
temp1>data = temp>data ;
temp>data = temp2 ;
}
}
}
display () ;
}
v o i d b ub bl e ( )
{

69

71

73

75

77

79

81

83

85

87

89

91

93

95

97

99

101

103

105

f o r ( node temp1 = head ; temp1 != NULL; temp1 = temp1>n e x t )


{
f o r ( node temp = head ; temp>n e x t != NULL; temp = temp>n e x t
)
{
i f ( temp>data > temp>next>data )
{
i n t temp2 ;
temp2 = temp>data ;
temp>data = temp>next>data ;
temp>next>data = temp2 ;
}
}
}
display () ;
}
void i n s e r t i o n ( )
{
f o r ( node i t = head ; i t != t a i l ; i t = i t >n e x t )
{
i n t v a l T o I n s = i t >data ;
node h o l e P o s = i t ;
w h i l e ( h o l e P o s >p r e v && v a l T o I n s < h o l e P o s >prev>data )
{
h o l e P o s >data = h o l e P o s >prev>data ;
h o l e P o s = h o l e P o s >p r e v ;
}
h o l e P o s >data = v a l T o I n s ;
}
display () ;
}
};
v o i d main ( )
{
sortings s ;
i n t data ;
do
{
c o u t << \ n1 ) i n s e r t \ n2 ) b ub bl e s o r t i n g \ n3 ) s e l e c t i o n \ n4 ) i n s e r t i o n
s o r t i n g \n ;

107

109

111

113

115

117

119

121

123

c o u t << ENTER CHOICE : ;


c i n >> data ;
c o u t << e n d l ;
s w i t c h ( data )
{
case 1:
s . insert () ;
system ( c l s ) ;
break ;
case 2:
c o u t << \nUNSORTED DATA : ;
s . display () ;
c o u t << \n\n ===== (BUBBLE) SORTED DATA ======= \n ;
s . b ub bl e ( ) ;
break ;
case 3:

c o u t << UNSORTED DATA : ;


s . display () ;
c o u t << \n\n ===== (SELECTION) SORTED DATA ======= \n ;
s . selection () ;
break ;
case 4:
c o u t << UNSORTED DATA : ;
s . display () ;
c o u t << \n\n ===== (INSERTION) SORTED DATA ======= \n ;
s . insertion () ;
break ;
}

125

127

129

131

133

135

} w h i l e ( data != 5 ) ;
getch () ;

137

139

You might also like