0% found this document useful (0 votes)
2 views3 pages

Lab 4

Uploaded by

Maaz Sayyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Lab 4

Uploaded by

Maaz Sayyed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment Number: 4

NAME: Md Muaz Sayyed ROLLNO: 60


CLASS: TY-IT-A BATCH: 3
___________________________________________________________________________
Problem Statement:
1. Find Majority Element from a given array
2. Make a table of n and execution time and graph of it.
Answer:
Pseudo Code:
Algorithm Majority(A[1..n]):
If |A| = 0 then output null, else if |A| = 1 then output A[1] ; else:
If n = |A| is odd then
check whether A[n] is a majority in A by counting the number of occurrences of
value A[n]; //O(n) work
if yes then output A[n], otherwise decrease n by one
Initialize additional array B of size |A|/2
Set j to 0
For i = 1,2,…,n/2 do:
if A[2i-1] = A[2i] then
increase j by one
set B[j] to A[2i]
Find if there is a majority in B[1..j] by executing Majority(B[1..j])
If a majority value x in B[1..j] is returned then check whether x is a majority in
A, by going through array A and counting the number of occurrences of value
x in A; if successful output x; otherwise null
Code:

#include <stdio.h>

int find_majority(int A[], int n) {


if (n == 0) {
return -1;
} else if (n == 1) {
return A[0];
} else {
int count = 0;
if (n % 2 == 1){
for (int i = 0; i < n; i++) {
if (A[i] == A[n - 1]) {
count++;
}
}
if (count > n / 2) {
return A[n - 1];
} else {
n--;
}
}

int B[n / 2];


int j = 0;

for (int i = 0; i < n; i += 2) {


if (A[i] == A[i + 1]) {
B[j] = A[i];
j++;
}
}

int majority_in_B = find_majority(B, j);

count = 0;
for (int i = 0; i < n; i++) {
if (A[i] == majority_in_B) {
count++;
}
}
if (count > n / 2) {
return majority_in_B;
} else {
return -1;
}
}
}

int main() {
int A[] = {3, 3, 4, 4, 4, 4, 2, 4};
int n = sizeof(A) / sizeof(A[0]);

int majority = find_majority(A, n);


if (majority != -1) {
printf("Majority element: %d\n", majority);
} else {
printf("No majority element found.\n");
}

return 0;
}

Time Complexity:
Best Case Scenario:
 Majority element at the last position A[n].
 As it is at the last position , occurrence of last element is counted first and
it exceeds n/2 and no further recursion is needed.
 Therefore, time complexity is O(n).

Worst Case Scenario:


If the majority element is not present or if it is present but requires the maximum
number of recursions.

T ( n )=running timeT ( n )=T ( n2 )+θ ( n) Number of recursive subproblems=1


n
¿ each subproblems= Time for all the non recursive steps=θ ( n ) Recursive tree method
2
n n n
treen¿ ¿ ¿ ...1
2 4 8
the complete tree :lognTime for non recursive steps=nTherefore , T ( n )=Ο(nlogn)

You might also like