java sort and inheritence

Solutions on MaxInterview for java sort and inheritence by the best coders in the world

showing results for - "java sort and inheritence"
Stefano
07 Oct 2019
1import java.util.*;
2
3class Arrange
4
5{
6
7 void sort(int a[])
8
9 {
10
11   int i,j,t=0;
12
13   for(i=0;i<a.length;i++)
14
15    for(j=0;j<a.length-i-1;j++)
16
17    {
18
19      if(a[j]>a[j+1])
20
21      {
22
23        t=a[j];
24
25        a[j]=a[j+1];
26
27        a[j+1]=t;
28
29       }
30
31     }
32
33  }  
34
35}
36
37// Class for searching a key in a sorted list of elements(inherits Arrange class)
38
39class Search extends Arrange
40
41{
42
43 Search(int x[],int key)
44
45 {
46
47  sort(x);
48
49  System.out.println("Sorted list of elements is=");
50
51  for(int i=0;i<x.length;i++)
52
53   System.out.println(" "+x[i]);
54
55  int low=0,high=x.length-1,mid;
56
57  while(low<=high)
58
59  {
60
61    mid=(low+high)/2;
62
63    if(x[mid]==key)
64
65    {
66
67    System.out.println("KEY FOUND");
68
69     break;
70
71    }
72
73    else
74
75     if(key<x[mid])
76
77       high=mid-1;
78
79     else
80
81            low=mid+1;
82
83  }
84
85  if(low>high)
86
87    System.out.println("KEY NOT FOUND");
88
89  }
90
91}
92
93// MAIN CLASS
94
95class Enter
96
97{
98
99 public static void main(String args[])
100
101 {
102
103   Scanner s=new Scanner(System.in);
104
105   System.out.println("Enter the number of elements you want to enter");
106
107   int size=s.nextInt();
108
109   System.out.println("Enter the Elements");
110
111   int x[]=new int[size];
112
113   for(int i=0;i<x.length;i++)
114
115     x[i]=s.nextInt();
116
117   System.out.println("Enter the key element you want to find");
118
119     int key=s.nextInt();
120
121   Search n=new Search(x,key);
122
123 }
124
125}
similar questions
queries leading to this page
java sort and inheritence