Skip to main content

Sorting an Array[Customized Logic ]

Sorting of array can be done through various logic's!! Either through using Arrays.sort() method or through customize logic. Here we are going to discuss the customized logic.  Below is the logic snippet for sorting the elements of an integer array

Logic for custom sort:

for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}

use of sort method:
Arrays.sort(arr);

Complete Code:

class SortingArray {
public void customSort(int [] arr)
{
int size=arr.length;
int temp=0;
// outer loop
for(int i=0;i<size-1;i++)
{
// inner loop
for(int j=0;j<size-1;j++)
{
if(arr[j]>arr[j+1])
{
/*
* Swapping logic
*/
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
// displaying the elements of the array
for(int i=0;i<size;i++)
{
System.out.println(arr[i]);
}
}
}

public class TestSortingArray {

public static void main(String[] args) {

// creating an object of SortingArray
SortingArray sortingArray = new SortingArray();
// invoke the method customSort and pass the array
sortingArray.customSort(new int[] { 9, 3, 2,2, 5, 4 });

}

}


# For using sort method in the above code, replace the outer and inner loop with Arrays.sort(arr) 

Input Array: 9,3,2,2,5,4
Output:




Comments

Popular posts from this blog

Read different types of data from standard input, process them as shown in output format and print the answer to standard output.

/* Problem Statement: Read different types of data from standard input, process them as shown in output format and print the answer to standard output. Input format: First line contains integer N. Second line contains string S. Output format: First line should contain N x 2. Second line should contain the same string S. Constraints: 0≤N≤10 1≤|S|≤15 where |S|= length of string S  */ import java.util.Scanner; public class TestClass {      public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int number = 0;         String string = null;         int firstInput = sc.nextInt();         String secondInput = sc.next();         // apply constraints         if (firstInput >= 0 && firstInput <= 1...

Basics of Git

Git is one among the popular open source version control software. This version control software helps you to track the changes in the source code or any text file. The changes can be in the terms of adding, modifying or deleting. All can be tracked through Git!. As per the official documentation " In Git, however, every developer is potentially both a node and a hub — that is, every developer can both contribute code to other repositories and maintain a public repository on which others can base their work and which they can contribute to ." which defines the distributed nature of it. Let's try to understand how it works. 1. Downloading and Installing Git: Based on the operating system, choose the installer file. Windows user can download it from the link  https://git-scm.com/ and Ubuntu users can type this command from terminal  "sudo apt-get install git" 2. Configuring Git: git config command is used to set Git configuration values on a global o...

Copying files from location to another in linux using Java

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; public class TestCopyFile { public static void main(String[] args) { File file1=new File(dummy path); String path=System.getenv("HOME"); File file2=new File(path+"/Desktop/dump/CSV_branch_master.csv"); System.out.println(path); System.out.println("after line2"); BufferedReader reader; PrintWriter writer; String line; try { System.out.println("inside try"); if(file2.createNewFile() || !file2.createNewFile()) { System.out.println("inside if"); reader =new BufferedReader(new FileReader(file1)); writer=new PrintWriter(new FileWriter(file2)); while((line=reader.readLine())!=null) { writer.println(line); } reader.close(); writer.close(); } }catch(Exception e) { System.out.println(e +"couldn...