0% found this document useful (0 votes)
13 views4 pages

Java Program Demos: Sorting & Counting

The document contains code snippets for several Java programs that demonstrate different algorithms and data structures including bubble sort, character counting, word counting, string reversal, number swapping, and uppercase character counting.

Uploaded by

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

Java Program Demos: Sorting & Counting

The document contains code snippets for several Java programs that demonstrate different algorithms and data structures including bubble sort, character counting, word counting, string reversal, number swapping, and uppercase character counting.

Uploaded by

kadalipavithra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

BubleSortDemo

package p3;

public class BubleSortDemo {

public static void main(String[] args) {


int arr[]= {7,8,3,1,2,5};
int temp=0;
for(int i=0;i<[Link]-1;i++)
{
for(int j=0;j<[Link]-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}

displayArray(arr);
}

private static void displayArray(int x[])


{
[Link]("Sorted Array !!");
for(int i=0;i<[Link];i++)
{
[Link](x[i]+" ");
}
}

}
-----------------------
DuplicateCharacterCountDemo
package p3;

import [Link];
import [Link];
// p=1 a=2 l=2 v=1 i=1
public class DuplicateCharacterCountDemo {

public static void main(String[] args) {

String s="pallavi";
char ch[]=[Link]();
HashMap<Character, Integer> charCount=new HashMap<Character,
Integer>();

for(int i=0;i<[Link];i++)
{
if([Link](ch[i]))
{
[Link](ch[i], [Link](ch[i])+1);
}
else
{
[Link](ch[i], 1);
}
}
[Link](charCount);

//Display Duplicate charactes only


Object[] arr=[Link]().toArray();
for(int i=0;i<[Link];i++)
{
if([Link](arr[i])>1)
{
[Link](arr[i]+" "+[Link](arr[i]));
}
}
//Unique Characters
for(int i=0;i<[Link];i++)
{
if([Link](arr[i])==1)
{
[Link](arr[i]+" "+[Link](arr[i]));
}
}
}

}
---------------------
DuplicateWordCountDemo
package p3;

import [Link];

public class DuplicateWordCountDemo {

public static void main(String[] args) {


String s="Java,Jre,JVM,Java,Jdk,Jdk,Java,Jira";
String str[]=[Link](",");
HashMap<String, Integer> wordCount=new HashMap<String, Integer>();

for(int i=0;i<[Link];i++)
{
if([Link](str[i]))
{
[Link](str[i], [Link](str[i])+1);
}
else
{
[Link](str[i], 1);
}
}

[Link]((k,v) -> [Link](k+" -->"+v));


}

}
-------------------
ReverseStringDemo
package p3;

import [Link];
public class ReverseStringDemo {

public static void main(String[] args) {


Scanner sc=new Scanner([Link]);
[Link]("Enter String :");
String s=[Link]();
char ch[]=[Link]();
String str="";
for(int i=[Link]-1;i>=0;i--)
{
str=str+ch[i];
}
[Link](str);
[Link]();
}

}
-----------------------
SwapNumbersWithoutTempVariable
package p3;

public class SwapNumbersWithoutTempVariable {

public static void main(String[] args) {


int first=20;
int second=10;
[Link]("Before Swap !!");
[Link]("first :"+first +" second :"+second);
//Start Swapping
first=first-second; //10
second=first+second; //20
first=second-first; //10
[Link]("After Swap !!");
[Link]("first :"+first +" second :"+second);

--------------------
SwapNumbersWithTempVariables
package p3;

public class SwapNumbersWithTempVariables {

public static void main(String[] args) {

int first=10;
int second=20;
[Link]("Before Swap !!");
[Link]("first :"+first +" second :"+second);
//Start Swapping
int temp=0;
temp=first;
first=second;
second=temp;
[Link]("After Swap !!");
[Link]("first :"+first +" second :"+second);
}

-----------------------
UppercaseCountDemo
package p3;

public class UppercaseCountDemo {

public static void main(String[] args) {

String s="AsDfGHjL";
int upperCount=0;
for(int i=0;i<[Link]();i++)
{
if([Link]([Link](i))==true)
{
upperCount=upperCount+1;
}
}
[Link]("# of Uppercase :"+upperCount);

}
-------------------
UppercaseCountDemo2
package p3;

public class UppercaseCountDemo2 {

public static void main(String[] args) {

String s="AsDfGHjL";
int count=0;
for(int i=0;i<[Link]();i++)
{
if([Link](i)>=65 && [Link](i)<=90)
{
count++;
}
}

[Link]("# of Uppercase :"+count);


}

Common questions

Powered by AI

Java's environment and libraries, such as util.Scanner for user input and String methods for array conversion, facilitate seamless string processing. In ReverseStringDemo, the Java standard library enables easy input management and character manipulation, confirming Java's robust support for string operations and user interaction, embodying its versatility and efficiency in handling typical tasks .

ReverseStringDemo reverses a string by converting it into a character array and then constructing a new string by appending each character in reverse order. The Scanner class facilitates user input by reading the string from the console, which is then processed for reversal .

The BubleSortDemo implements the bubble sort algorithm, which organizes an array by comparing each adjacent pair of elements and swapping them if they are in the wrong order. This process repeats iteratively, moving larger elements towards the end of the array, effectively 'bubbling' them up to their correct position. The algorithm employs a basic principle of comparing and swapping until the list is sorted .

DuplicateWordCountDemo calculates term frequency by splitting a string into an array of words and then using a HashMap to keep track of how many times each word appears. A HashMap is appropriate because it allows for efficient insertion, lookup, and update of word counts by establishing a constant time complexity on average for these operations .

UppercaseCountDemo uses the Character class method .isUpperCase() to identify and count uppercase letters. UppercaseCountDemo2, on the other hand, uses ASCII value comparison to achieve the same result, checking if a character falls within the ASCII range of uppercase letters (65-90). Both methods result in counting uppercase letters but differ in implementation specifics .

Displaying the sorted array is necessary for verification purposes, ensuring that the bubble sort algorithm executed correctly. This is implemented using a separate method, displayArray, which iterates through the sorted array, outputting each element in sequence, thereby confirming the sort's success .

The DuplicateCharacterCountDemo extracts and displays unique characters by iterating over the character counts stored in a HashMap. It recognizes unique characters by isolating those with a count equal to one and prints these, effectively filtering out duplicates .

DuplicateCharacterCountDemo uses a HashMap to store characters as keys and their counts as values from a given string. It iterates over each character, incrementing the count for each duplicate instance encountered. The program further isolates and prints those characters that have a count greater than one, representing duplicates .

Swapping numbers without a temporary variable can be more memory efficient as it does not require extra storage, an important consideration in environments with strict memory constraints. However, it introduces additional arithmetic operations, which might be less intuitive. Using a temporary variable simplifies the logic and minimizes arithmetic operations, especially in high-level scenarios or when readability is prioritized over marginal memory savings .

SwapNumbersWithoutTempVariable uses arithmetic operations to swap values without a temporary variable, involving three arithmetic operations. It has a constant time complexity, O(1), similar to the SwapNumbersWithTempVariables approach, which uses a temporary variable for swapping. The choice can depend on the specific use case or memory constraints, but both are efficient in terms of time complexity .

You might also like