Pages

Monday, August 18, 2025

Sorting an ArrayList in Java - optimized version of sort(use if not asked in question)

 // import java.util.ArrayList;

import java.util.*;
// import java.util.Collections;
public class Classroom {

    public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<>();
       list.add(2);
       list.add(5);
       list.add(9);
       list.add(6);
       list.add(8);

   
       System.out.println("List before sorted: " + list );
       Collections.sort(list);
       System.out.println("List after sorted: " + list);

       // desc order
       Collections.sort(list, Collections.reverseOrder());
       
       // Comparator - fnx logic
       System.out.println("in Desc Order: " + list);
    }

}

No comments:

Post a Comment

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...