Skip to main content
    public static void main(String[] args) {


        MyLinkedList list = new MyLinkedList();

        Student s = new Student(1, "John", 20, "Italy", "2011");
        list.addStudent(s);
        Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
        list.addStudent(s2);
        Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
        list.addStudent(s3);

        System.out.println("Students in the list: ");
        list.print();

        Node[] n = list.convertA(list);
        quickSort(n, 0, (n.length-1));
        System.out.println("Sorted list is:");

        for(int q =0;q<n.length;q++){
            System.out.println(n[q] + " ");
        }
    }

    public static int partition(Node arr[], int left, int right) {

        int i = left, j = right;

        Node tmp;

        Node pivot = arr[(left + right) / 2];

        while (i <= j) {

            while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
                i++;
            }

            while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
                j--;
            }

            if (i <= j) {

                tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
                i++;
                j--;

            }

        }
        return i;

    }

    public static void quickSort(Node arr[], int left, int right) {

        int index = partition(arr, left, right-1);

        if (left < index - 1) {
            quickSort(arr, left, index - 1);
        }

        if (index < right) {
            quickSort(arr, index, right);
        }
    }
public static void main(String[] args) {


MyLinkedList list = new MyLinkedList();

Student s = new Student(1, "John", 20, "Italy", "2011");
list.addStudent(s);
Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
list.addStudent(s2);
Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
list.addStudent(s3);

System.out.println("Students in the list: ");
list.print();

 Node[] n = list.convertA(list);
  quickSort(n, 0, (n.length-1));
  System.out.println("Sorted list is:");

  for(int q =0;q<n.length;q++){
      System.out.println(n[q] + " ");
  }
}

public static int partition(Node arr[], int left, int right) {

int i = left, j = right;

Node tmp;

Node pivot = arr[(left + right) / 2];

while (i <= j) {

    while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
        i++;
    }

    while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
        j--;
    }

    if (i <= j) {

        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
        i++;
        j--;

    }

}
return i;

}

public static void quickSort(Node arr[], int left, int right) {

int index = partition(arr, left, right-1);

if (left < index - 1) {
    quickSort(arr, left, index - 1);
}

if (index < right) {
    quickSort(arr, index, right);
}
  }
    public static void main(String[] args) {


        MyLinkedList list = new MyLinkedList();

        Student s = new Student(1, "John", 20, "Italy", "2011");
        list.addStudent(s);
        Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
        list.addStudent(s2);
        Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
        list.addStudent(s3);

        System.out.println("Students in the list: ");
        list.print();

        Node[] n = list.convertA(list);
        quickSort(n, 0, (n.length-1));
        System.out.println("Sorted list is:");

        for(int q =0;q<n.length;q++){
            System.out.println(n[q] + " ");
        }
    }

    public static int partition(Node arr[], int left, int right) {

        int i = left, j = right;

        Node tmp;

        Node pivot = arr[(left + right) / 2];

        while (i <= j) {

            while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
                i++;
            }

            while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
                j--;
            }

            if (i <= j) {

                tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
                i++;
                j--;

            }

        }
        return i;

    }

    public static void quickSort(Node arr[], int left, int right) {

        int index = partition(arr, left, right-1);

        if (left < index - 1) {
            quickSort(arr, left, index - 1);
        }

        if (index < right) {
            quickSort(arr, index, right);
        }
    }
added 11 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Code review for this please! Review of linked list and quick sort

I was trying another approach to sortof sorting a linked list. Aside from the available methods, iI've decided to take each node from the linked list and place it ininto an array and by. With that i, I would be able to compare the data variables easily. I applied the quickSortquickSort() on the array, and this is what i got..I have.I I would appreciate any feedback/comments on my code.

public static void main(String[] args) {


MyLinkedList list = new MyLinkedList();

Student s = new Student(1, "John", 20, "Italy", "2011");
list.addStudent(s);
Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
list.addStudent(s2);
Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
list.addStudent(s3);

System.out.println("Students in the list: ");
list.print();

 Node[] n = list.convertA(list);
  quickSort(n, 0, (n.length-1));
  System.out.println("Sorted list is:");

  for(int q =0;q<n.length;q++){
      System.out.println(n[q] + " ");
  }
}

public static int partition(Node arr[], int left, int right) {

int i = left, j = right;

Node tmp;

Node pivot = arr[(left + right) / 2];

while (i <= j) {

    while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
        i++;
    }

    while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
        j--;
    }

    if (i <= j) {

        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
        i++;
        j--;

    }

}
return i;

}

public static void quickSort(Node arr[], int left, int right) {

int index = partition(arr, left, right-1);

if (left < index - 1) {
    quickSort(arr, left, index - 1);
}

if (index < right) {
    quickSort(arr, index, right);
}
  }

Code review for this please!

I was trying another approach to sort a linked list. Aside from the available methods, i decided to take each node from the linked list and place it in an array and by that i would be able to compare the data variables easily. I applied the quickSort on the array and this is what i got...I would appreciate any feedback/comments on my code

public static void main(String[] args) {


MyLinkedList list = new MyLinkedList();

Student s = new Student(1, "John", 20, "Italy", "2011");
list.addStudent(s);
Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
list.addStudent(s2);
Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
list.addStudent(s3);

System.out.println("Students in the list: ");
list.print();

 Node[] n = list.convertA(list);
  quickSort(n, 0, (n.length-1));
  System.out.println("Sorted list is:");

  for(int q =0;q<n.length;q++){
      System.out.println(n[q] + " ");
  }
}

public static int partition(Node arr[], int left, int right) {

int i = left, j = right;

Node tmp;

Node pivot = arr[(left + right) / 2];

while (i <= j) {

    while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
        i++;
    }

    while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
        j--;
    }

    if (i <= j) {

        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
        i++;
        j--;

    }

}
return i;

}

public static void quickSort(Node arr[], int left, int right) {

int index = partition(arr, left, right-1);

if (left < index - 1) {
    quickSort(arr, left, index - 1);
}

if (index < right) {
    quickSort(arr, index, right);
}
  }

Review of linked list and quick sort

I was trying another approach of sorting a linked list. Aside from the available methods, I've decided to take each node from the linked list and place it into an array. With that, I would be able to compare the data variables easily. I applied quickSort() on the array, and this is what I have. I would appreciate any feedback/comments on my code.

public static void main(String[] args) {


MyLinkedList list = new MyLinkedList();

Student s = new Student(1, "John", 20, "Italy", "2011");
list.addStudent(s);
Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
list.addStudent(s2);
Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
list.addStudent(s3);

System.out.println("Students in the list: ");
list.print();

 Node[] n = list.convertA(list);
  quickSort(n, 0, (n.length-1));
  System.out.println("Sorted list is:");

  for(int q =0;q<n.length;q++){
      System.out.println(n[q] + " ");
  }
}

public static int partition(Node arr[], int left, int right) {

int i = left, j = right;

Node tmp;

Node pivot = arr[(left + right) / 2];

while (i <= j) {

    while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
        i++;
    }

    while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
        j--;
    }

    if (i <= j) {

        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
        i++;
        j--;

    }

}
return i;

}

public static void quickSort(Node arr[], int left, int right) {

int index = partition(arr, left, right-1);

if (left < index - 1) {
    quickSort(arr, left, index - 1);
}

if (index < right) {
    quickSort(arr, index, right);
}
  }
Source Link
Scarl
  • 135
  • 1
  • 1
  • 6

Code review for this please!

I was trying another approach to sort a linked list. Aside from the available methods, i decided to take each node from the linked list and place it in an array and by that i would be able to compare the data variables easily. I applied the quickSort on the array and this is what i got...I would appreciate any feedback/comments on my code

public static void main(String[] args) {


MyLinkedList list = new MyLinkedList();

Student s = new Student(1, "John", 20, "Italy", "2011");
list.addStudent(s);
Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
list.addStudent(s2);
Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
list.addStudent(s3);

System.out.println("Students in the list: ");
list.print();

 Node[] n = list.convertA(list);
  quickSort(n, 0, (n.length-1));
  System.out.println("Sorted list is:");

  for(int q =0;q<n.length;q++){
      System.out.println(n[q] + " ");
  }
}

public static int partition(Node arr[], int left, int right) {

int i = left, j = right;

Node tmp;

Node pivot = arr[(left + right) / 2];

while (i <= j) {

    while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
        i++;
    }

    while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
        j--;
    }

    if (i <= j) {

        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
        i++;
        j--;

    }

}
return i;

}

public static void quickSort(Node arr[], int left, int right) {

int index = partition(arr, left, right-1);

if (left < index - 1) {
    quickSort(arr, left, index - 1);
}

if (index < right) {
    quickSort(arr, index, right);
}
  }