ソートアルゴリズム
13668 ワード
LeetCodeの912題配列ソートをテストとし,結果が小さいものから大きいものへのソートが要求される.
バブルソート
バブルソート class Solution {
public int[] sortArray(int[] nums) {
for(int i=0; i<nums.length; i++){
for(int j=0; j<nums.length-i-1; j++){
if(nums[j] > nums[j+1]){
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
return nums;
}
}
ソートの選択 class Solution {
public int[] sortArray(int[] nums) {
int minIndex,temp;
for(int i=0; i<nums.length; i++){
minIndex = i;
for(int j=i+1; j<nums.length; j++){
if(nums[j] < nums[minIndex])
minIndex = j;
}
temp = nums[i];
nums[i] = nums[minIndex];
nums[minIndex] = temp;
}
return nums;
}
}
クイックソート class Solution {
public int[] sortArray(int[] nums) {
return sort(nums,0,nums.length-1);
}
private int[] sort(int[] nums, int low,int high){
int k = partition(nums,low,high);
if(k>low) sort(nums,low,k-1);
if(k<high) sort(nums,k+1,high);
return nums;
}
private int partition(int nums[], int low,int high){
int point = nums[low];
while(low < high){
while(low < high && nums[high] >= point)
high--;
swap(nums,low,high);
while(low < high && nums[low] <= point)
low++;
swap(nums,low,high);
}
return low;
}
private void swap(int[] nums,int low,int high){
int temp = nums[low];
nums[low] = nums[high];
nums[high] = temp;
}
}
class Solution {
public int[] sortArray(int[] nums) {
for(int i=0; i<nums.length; i++){
for(int j=0; j<nums.length-i-1; j++){
if(nums[j] > nums[j+1]){
int temp = nums[j];
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
return nums;
}
}
class Solution {
public int[] sortArray(int[] nums) {
int minIndex,temp;
for(int i=0; i<nums.length; i++){
minIndex = i;
for(int j=i+1; j<nums.length; j++){
if(nums[j] < nums[minIndex])
minIndex = j;
}
temp = nums[i];
nums[i] = nums[minIndex];
nums[minIndex] = temp;
}
return nums;
}
}
クイックソート class Solution {
public int[] sortArray(int[] nums) {
return sort(nums,0,nums.length-1);
}
private int[] sort(int[] nums, int low,int high){
int k = partition(nums,low,high);
if(k>low) sort(nums,low,k-1);
if(k<high) sort(nums,k+1,high);
return nums;
}
private int partition(int nums[], int low,int high){
int point = nums[low];
while(low < high){
while(low < high && nums[high] >= point)
high--;
swap(nums,low,high);
while(low < high && nums[low] <= point)
low++;
swap(nums,low,high);
}
return low;
}
private void swap(int[] nums,int low,int high){
int temp = nums[low];
nums[low] = nums[high];
nums[high] = temp;
}
}
class Solution {
public int[] sortArray(int[] nums) {
return sort(nums,0,nums.length-1);
}
private int[] sort(int[] nums, int low,int high){
int k = partition(nums,low,high);
if(k>low) sort(nums,low,k-1);
if(k<high) sort(nums,k+1,high);
return nums;
}
private int partition(int nums[], int low,int high){
int point = nums[low];
while(low < high){
while(low < high && nums[high] >= point)
high--;
swap(nums,low,high);
while(low < high && nums[low] <= point)
low++;
swap(nums,low,high);
}
return low;
}
private void swap(int[] nums,int low,int high){
int temp = nums[low];
nums[low] = nums[high];
nums[high] = temp;
}
}