-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallbig.java
More file actions
144 lines (143 loc) · 4.62 KB
/
Copy pathSmallbig.java
File metadata and controls
144 lines (143 loc) · 4.62 KB
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
139
140
141
142
143
144
/*3 Write a program in java that stores 20 integers entered by a user in an array, and
then computes the following using appropriate methods:
(i) Smallest and Biggest elements
(ii) nth biggest and nth smallest element
(iii) Position of smallest and biggest element */
import java.util.Scanner;
import java.util.Arrays;
class Smallbig
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
int size=sc.nextInt();
int nums[]=new int[size];
int nums1[]=new int[size];
System.out.println("enter the elements of the array");
//TIME COMPLEXCITY=O(n)
for(int i=0;i<size;i++)
{
nums[i]=sc.nextInt();
nums1[i]=nums[i];
}
//Sort nums1 using Quick Sort
quickSort(nums1, 0, size - 1);
System.out.println("PRINTING THE SMALLEST AND BIGGEST ELEMENT");
System.out.println("The smallest element is:"+nums1[0]);
System.out.println("The biggest element is"+nums1[size-1]);
System.out.println();
System.out.println("PRINTING THE NTH SMALLEST AND BIGGEST");
//nth smallest elements
int j=1;
for(int i =0;i<size;i++)//O(n)
{
//handeling the duplicates
if (i==0||nums1[i]!=nums1[i - 1])
{
System.out.println(j+"th smallest element is:"+nums1[i]);
j++;
}
}
System.out.println();
//nth biggest elements
j=1;
for(int i=size-1;i>=0;i--)//O(n)
{
//handeling the duplicates
if (i==size-1||nums1[i]!=nums1[i + 1])
{
System.out.println(j+"th biggest element is:"+nums1[i]);
j++;
}
}
int biggest=nums[size-1];
int smallest=nums[0];
System.out.println();
System.out.println("PRINTING THE POSITIONS OF SMALLEST AND BIGGEST ELEMENTS");
//binary searsch find the postion of the smallestv element.
int index = binarySearch(nums, smallest);
if(index!=-1)
{
System.out.println("The smallest element is found at"+index+"th postion");
}
else
{
System.out.println("the element is not found");
}
//binary searsch find the postion of the biggest element.
index = binarySearch(nums, biggest);
if(index!=-1)
{
System.out.println("The biggest element is found at"+index+"th postion");
}
else
{
System.out.println("the element is not found");
}
}
//Method to partition the array and return the pivot index
public static int partition(int[] array,int low,int high)
{
int pivot=array[high];//Choosing the rightmost element as pivot
int i=low-1;//Index of smaller element
for(int j=low;j<high;j++)
{
//If the current element is smaller than the pivot
if(array[j]<pivot)
{
i++;
//Swap array[i] and array[j]
int temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
//Swap array[i + 1] with the pivot (array[high])
int temp=array[i + 1];
array[i+1]=array[high];
array[high]=temp;
return i+1;//Return the pivot index
}
//TIMECOMPLEXICITY OF QUICK SORT IS ==O(n log n)
//QuickSort algorithm
public static void quickSort(int[] array,int low,int high)
{
if (low<high)
{
//Find the pivot index
int pivotIndex=partition(array,low,high);
//Recursively sort the left and right subarrays
quickSort(array,low,pivotIndex-1);
quickSort(array,pivotIndex+1,high);
}
}
//TIME COMPLEXCITY OF BINARY SEARCH IS=O(log n)
public static int binarySearch(int[] array,int target)
{
int low=0;
int high=array.length- 1;
while (low<=high)
{
int mid=low+(high-low)/2;
//Check if target is present at mid
if(array[mid]==target)
{
return mid; // Target found
}
//If target is smaller, ignore right half
if(array[mid]>target)
{
high=mid-1;
}
else
{
//If target is greater, ignore left half
low=mid+1;
}
}
//Target element not found
return -1;
}
}
//TIME COMPLEXICITY =O(n log n)+O(n)