-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assignment Ques
138 lines (110 loc) · 2.54 KB
/
Assignment Ques
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
##ALL ARE WRITTEN IN CPP
Given an array A, write an algorithm to find the number of inversions in the array A. If (i < j)
and (A[i] > A[j]) then the pair (i, j) is called an inversion of an array A.
For example,
Input: A[] =[1, 9, 6, 4, 5]
Output: Inversion count is 5
There are 5 inversions in the array - (9, 6), (9, 4), (9, 5), (6, 4), (6, 5)
SOLN
#include<iostream>
using namespace std;
int inverse(int n,int a[])
{ int count=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i<j)
{
if(a[i]>a[j])
count++;
}
}
}
return count;
}
int main()
{
int n;
cout<<"ENTER THE SIZE OF ARRAY: \n";
cin>>n;
int a[n];
cout<<"ENTER THE ARRAY ELEMENTS: "<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
int k=inverse(n,a);
cout<<"THE NUMBER OF PAIRS FOUND IS "<<k;
return 0;
}
Write an efficient algorithm to sort the array of binary elements.
For example,
Input: A [] =[1, 0, 1, 0, 1, 1, 0]
Output: Array after sorting A []=[0, 0, 0, 1, 1, 1, 1]
SOLN
#include<iostream>
using namespace std;
int sortbinary(int n,int arr[])
{
int j = -1,temp;
for (int i = 0; i < n; i++) {
if (i!=j&&arr[i] < 1){
j++;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
for (int i = 0; i < n; i++)
cout<< arr[i]<<"\t";
}
int main()
{ int n;
cout<<"ENTER THE SIZE OF ARRAY: \n";
cin>>n;
int arr[n];
cout<<"ENTER THE ARRAY ELEMENTS: "<<endl;
for(int i=0;i<n;i++)
cin>>arr[i];
sortbinary(n,arr);
return 0;
}
Write selection sort and insertion sort algorithms to arrange the nodes of the given singly
linked list in the ascending order.
For example:
Input: 2 —> 3 —>15—>1—>2—>0—>8—>NULL
Output: 0—> 1—>2—>2—>3—>8—>15—>NULL
SOLN
#include <iostream>
class Node {
public:
Node *next;
int data;
};
typedef Node * ListType;
void insertionSort(ListType &list) {
ListType *p = &list;
while ( (*p)->next && (*p)->next->data < (*p)->data)
{
ListType node= *p;
*p=node->next;
node->next=node->next->next;
(*p)->next=node;
p= &(*p)->next;
}
}
int main()
{
Node *head=0;
int n;
while (std::cout << ">>> ", std::cin >> n)
{
Node *p=new Node;
p->data=n;
p->next=head;
head=p;
insertionSort(head);
for (p=head; p; p=p->next)
std::cout << p->data << " ";
std::cout << std::endl;
}
}