Sorting Arrays

1
2
Arrays Part 1
PREVIEW18m 56s
3

## The course is part of this learning path

Start course
Difficulty
Beginner
Duration
1h 15m
Students
65
Ratings
5/5
Description

In this course, we'll learn about Arrays in Java.

### Learning Objectives

• What is an Array?
• Declaration and Initialization
• Sorting Arrays
• Searching Arrays
• Multi-Dimensional Arrays

### Intended Audience

• Anyone looking to get Oracle Java Certification
• Those who want to learn the Java Programming language from scratch
• Java developers who want to increase their knowledge
• Beginners with no previous coding experience in Java programming
• Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

### Prerequisites

• No prior knowledge is required about the Java programming language.
• Basic computer knowledge
Transcript

Hi there. In this lesson, we'll learn to sort the elements of an array from smallest to largest, i.e. ascending. For example, we can arrange the elements of an array of int data types from smallest to largest. This is called sorting arrays in the literature. You can do these sorting operations with the methods you write yourself, or you can use the created sort method of the Arrays class. We'll use the sort method in this lesson. If you're ready, let's move on to the Eclipse and get some practice. In the exercise project, I right click on the 'array' package and select the new class. This class name can be SortingArray and check the checkbox for the main method and click the 'Finish' button.

Okay, first let's look at the sorting of the int array. I'll create an int array. int[] numbers = {}. So, the elements of this array can be 5, 3, 10, 30, 1, 50. Now, let's sort the elements of this array using the sort method of the Arrays class. Here I write Arrays.sort. And in parentheses, I write the name of the array i.e. numbers. And let's print the numbers array on the console. System.out.printIn(Arrays.toString(numbers)). And let's run the code and see. As you can see, the elements are sorted in ascending order.  1, 3, 5, 10, 30, 50. Okay, let's create a double array and sort it. The name of this array can be myDoubles. And its elements can be 1.2, 3.5, -2.5, 0.7, 10.0, 11.

Let's sort it. Arrays.sort(myDoubles). And let's print the elements of this array on the console. System.out.printIn(Arrays.toString(myDoubles)). Let's run the code and test it. As you can see, the elements are sorted in ascending order again. -2.5, 0.7, 1.2, 3.5, 10.0, 11.0. Now, let's sort the char array. The name of the array can be myChars, and its elements can be a, B, A, F, b, E. Let's sort it. Arrays.sort(myChars). And let's print the elements of this array on the console. System.out.printIn(Arrays.toString(myChars)). Let's run the code and test it. Notice that it sorts the uppercase letters first, then the lower case letters because capital letters start first according to the ASCII table. For example, the value of uppercase A is 65, while the value of lowercase a is 97.

Okay, let's sort the string array. The name of the array can be myCars and its elements can be Ferrari, Opel, Mercedes, BMW, Ford, hyundai. The first letter of the Hyundai is lowercase. Let's sort it. Arrays.sort(myCars). And let's print the elements of this array on the console. System.out.printIn(arrays.toString(myCars)). Let's run the code and test it. This time they sorted it in alphabetical order. Alphabetical order is taken into account when sorting strings. But as you can see, the Hyundai is the last element because it begins with a lowercase letter, and always the lowercase is greater than the uppercase in sorting. Okay, let's do another example of sorting strings. The name of the array can be myNums and its elements can be 50, 70, 5, 90, 9, 60. Let's sort it. Arrays.sort(myNums). And let's print the elements of this array on the console. System.out.printIn(arrays.toString(myNums)). Let's try to guess the result before running the code.

This time our strings consist of numbers, not letters. So, the logic here is it looks at the first digit starting from the left first. So, five for the first element, seven for the second element, five for the third element, nine for the fourth element, nine again for the fifth element, and six for the last element. Sorts them first. In this case the first two elements will be 5 or 50. The third element will be 60. The fourth element will be 70 and the fifth and sixth elements will be 9 or 90. If we look at 5 and 50, since they start with the same number from the left, they are both five. In this case, it looks at the second number from the left, but since five consists of a single digit, it has no equivalent in the second digit. So, five is smaller. So, the first two elements will be 5 and 50 respectively.

Similarly, when we compare the last two elements, 9 becomes less than 90. So, the fifth element would be 9 and the last element would be 90. Now, let's run the application and test it. As you can see, first 5, then 50, then 60, then 70, then 9, and finally, 90 were printed. Also, let's add two more elements to this array. The seventh element will be lowercase a, and the eighth element will be capital Z. So, if the string arrays contain letters and numbers, this time, the letters will sort after the numbers. And the rule is always capital letters less than the lowercase letter in sorting.

So, the seventh element must be capital Z. And the last element must be lowercase a. Let's run and see. As you can see, the seventh element is uppercase Z and the eighth element is lowercase a. The thing to keep in mind is this: if the array consists of numeric values, it's sorted from largest to smallest. If it consists of characters, first uppercase letters, then lowercase letters are sorted. If the type of array is the string, it's sorted alphabetically. However, if the elements in a string represent numeric numbers, this time it compares the numbers among themselves on the condition that it starts from the left and sorts the elements of the array accordingly. Yes, the sorting elements are like this. Let's take a short break here. See you in the next lesson. 