0% found this document useful (0 votes)
43 views2 pages

Class Ascending - ISC COMPUTER PROJECT 2018: Import Import Class Int

This document describes a Java class called Ascending that takes in integer arrays, sorts them in ascending order, and merges them into a single sorted array. The Ascending constructor takes in an integer size and reads that number of integers from user input into an array. The merge method takes two Ascending objects, iterates through their arrays, and adds the smaller element to the merged array, incrementing the respective index each time, until both source arrays are traversed. It returns a new Ascending object containing the merged and sorted array. The main method demonstrates creating two Ascending objects, merging them, and displaying the result.

Uploaded by

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

Class Ascending - ISC COMPUTER PROJECT 2018: Import Import Class Int

This document describes a Java class called Ascending that takes in integer arrays, sorts them in ascending order, and merges them into a single sorted array. The Ascending constructor takes in an integer size and reads that number of integers from user input into an array. The merge method takes two Ascending objects, iterates through their arrays, and adds the smaller element to the merged array, incrementing the respective index each time, until both source arrays are traversed. It returns a new Ascending object containing the merged and sorted array. The main method demonstrates creating two Ascending objects, merging them, and displaying the result.

Uploaded by

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

Class Ascending - ISC COMPUTER PROJECT 2018 1/2

1 import java.io.*;
2 import java.util.Scanner;
3
4 class Ascending
5 {
6 int a1[], size;
7
8 public Ascending(){}
9
10 public Ascending (int n) throws IOException
11 {
12 Scanner sc=new Scanner (System.in);
13 int i, value;
14 size=n;
15 a1=new int[size];
16
17 for(i=0; i<=n-1; i++)
18 {
19 value=sc.nextInt();
20 a1[i]=value;
21 }
22 System.out.println("Input taking ends");
23 }
24
25 public void displayList()
26 {
27 int i;
28 for (i=0; i<=size-1; i++)
29 {
30 System.out.print( a1[i] + " ");
31 }
32 }
33
34 Ascending merge (Ascending as) throws Exception
35 {
36 Ascending mergedObject=new Ascending();
37 int fi=0, si=0, ti=0; /* fi first index for calling o
bject; si second index for object passed as argument; ti third ind
ex for object to be created */
38 int i;
39 mergedObject.a1=new int [this.size+as.size];
40 //mergedObject.size= this.size + as.size;
41
42 while (fi<=this.size-1 && si<=as.size-1)
43 {
44
45 if (this.a1[fi] < as.a1[si] )
46 {
47 if ( ti>0 && this.a1[fi] == mergedObject.a1[ti-1]
) // checking if the same value already transferred to mergedObje
ct
48 fi++;
49 else

5 Aug, 2017 10:06:51 PM


Class Ascending - ISC COMPUTER PROJECT 2018 (continued) 2/2

50 {
51 mergedObject.a1[ti]= this.a1[fi];
52 fi++; ti++;
53 }
54 }
55 else
56 {
57 if( ti>0 && as.a1[si] == mergedObject.a1[ti-1] )
58 si++;
59 else
60 {
61 mergedObject.a1[ti]=as.a1[si];
62 si++; ti++;
63 }
64 }
65 }
66
67 if( fi==this.size) /* indicated all elements of calling ob
ject shifted to mergedObject */
68 {
69 for (i=si; i<=as.size-1; i++)
70 {
71 if( as.a1[i] != mergedObject.a1[ti-1] )
72 mergedObject.a1[ti++]= as.a1[i];
73 }
74 }
75 else
76 if (si==as.size)
77 {
78 for(i=fi; i<=this.size-1; i++)
79 {
80 if( this.a1[i] != mergedObject.a1[ti-1] )
81 mergedObject.a1[ti++]=this.a1[i];
82 }
83 }
84
85 mergedObject.size=ti;
86 return mergedObject;
87 }
88
89 public static void main(String arg[]) throws Exception
90 {
91 Ascending asc=new Ascending(4);
92 Ascending asc1=new Ascending(4);
93
94 Ascending ascMerged= asc.merge(asc1);
95
96 ascMerged.displayList();
97 }
98 }
99

5 Aug, 2017 10:06:52 PM

You might also like