欢迎使用CSDN-markdown编辑器

public static int[] selectionSort(int[] arr){
    int n = arr.length;
    for (int x = 0; x < n; x++){
        int index_of_min = x;
        for (int y = x; y < n; y++){
            if(arr[index_of_min] > arr[y]){ //bug: >arr[y] instead of >arr[x]
                index_of_min = y;
            }
        }
        int temp = arr[x];
        arr[x] = arr[index_of_min];
        arr[index_of_min] = temp;
    }
    return arr;
}


public static int[] reverseArray(int[] arr){
    int i, temp, originalLen = arr.length;
    int len = originalLen;
    for(i = 0; i < (originalLen/2); i++){
        temp = arr[len - i - 1];    //bug: arr[len-i-1] instead of arr[len-1]
        arr[len - i - 1] = arr[i];  //bug: arr[len-i-1] instead of arr[len-1]
        arr[i] = temp;
        //len += 1;     make this line as comment
    }
    return arr;
}


public static int[] replaceValues(int arr[]){
    int i, j, len =  arr.length;
    if (len % 2 == 0){
        for(i = 0; i < len; i++){   //i < len, not <=
            arr[i] = 0;
        }
    }else{
        for(j = 0; j < len; j++){   //j < len, not <=
            arr[j] = 1;
        }
    }
    return arr;
}


import java.util.*;
import java.lang.*;
import java.io.*;


public static int removeElement (int arr[], int index)
{
    int i,j,len = arr.length;

    //case of index less than zero
    if(index < 0){
        System.out.println(arr);
    }

    //original code
    else if (index < len){
        for(i = index; i < len - 1; i++){
            //bug: not array[i++] but array[i+1]
            arr[i] = arr[i+1];
        }
        int rarr[] =  new int[len-1];
        for(i = 0; i < len-1; i++){
            rarr[i] = arr[i];
        }
        System.out.println(Arrays.toString(rarr));
    }
    else{
        System.out.println(Arrays.toString(arr));
    }
}


public class removeDuplicates {
    public static int[] remove(int[] arr){ 

        int end = arr.length;

        for(int i = 0; i < end; i++){
            for(int j = i + 1; j < end; j++){   //bug: loop from j=i+1 instead of i
                if(arr[i] == arr[j]){                  
                    for(int k = j+1; k < end; k++){ //bug: k from j+1 instead of j
                        arr[k-1] = arr[k];
                    }
                    end--;
                    j--;
                }
            }
        }

        int[] whitelist = new int[end];
        for(int i = 0; i < end; i++){
            whitelist[i] = arr[i];
        }
        System.out.print("new length is ");
        System.out.println(end);
        return whitelist;
    }

    public static void main(String[] args) {
        int[] arr = {3, 2, 1, 3, 2, 1, 3, 2, 1};
        remove(arr);
        for(int i:arr){
            System.out.print(i);
            System.out.print(", ");
        }
    }
}



public static void print3(int row){
    int x = 1;
    for (int i = 1; i <= row; i++){     //omit {}
        for(int j = i; j > 0; j--){
            System.out.print(x + "" + x);
        }
        System.out.println();
    }
}


public static void print2(int row){
    for (int i = 1; i <= row; i++){
        char ch = 'a';
        char print = ch;
        for (int j = 1; j <=i; j++){    //bug: j=1 instead of j=0
            System.out.print(print++);  //bug: print++ instead of ch++,
                                        //delete one pair of brackets for ((print++))
        }
        System.out.println();
    }
}


public static int[] ManchesterArray(int[] arr){
    //add case of ret[0]
    result = (A[i] != A[i-1])   //change == to !=
}



public static int[] insertionSort(int[] arr){
    int n = arr.length;
    for (int i = 1; i < n; i++){
        if(arr[i - 1] > arr[i]){    //bug: > instead of <
            int temp = arr[i];
            int j = i;
            while(j > 0 && arr[j-1] > temp){    //bug: > instead of <
                arr[j] = arr[j-1];
                j--;
            }
            arr[j] = temp;
        }
    }
    return arr;
}


public static void print4(int num){
    int i, print = 0;
    if(num % 2 == 0){
        print = 0;
        for(i = 0; i < num; i++){   //omit {}
            System.out.print(print + " ");
            print += 2;
        }
    }else{
        print = 1;
        for(i = 0; i < num; i++){   //omit {}
            System.out.print(print + " ");
            print += 2;
        }
    }
}

public int find(int num){
    int count = 0;
    //bug: set a numcopy for calculation, set a case for num=0
    int numcopy = num;
    if(num == 0){
        return 0;
    }
    while(num != 0){
        num = num/10;
        count++;
    }
    //return num%count;
    return numcopy%count;

}


public static int[] bubbleSort(int[] arr){
    int n = arr.length;
    for (int x = 0; x < n; x++){
        for (int y = x; y < n; y++){
            if(arr[x] > arr[y]){    //bug: >arr[y] isntead of >arr[x]
                int temp = arr[x];
                arr[x] = arr[y];
                arr[y] = temp;
            }
        }
    }
    return arr;
}

public static int[] ArraySort(int[] arr){
    int i, max, location, j, temp, len = arr.length;
    for(i = 0; i < len; i++){
        max = arr[i];
        location = i;
        for (j = i+1; j < len; j++){    //j=i has redundant calculation
            if(max < arr[j]){   //bug: from max to min, using <
                max = arr[j];
                location = j;
            }
        }
        temp = arr[i];
        arr[i] = arr[location];
        arr[location] = temp;
    }
    return arr;
}



public static int[] sortArray(int[] arr){
    int len = arr.length;
    int i, j, temp; //unused variables "small" and "pos"

    for(i = 0; i < len-1; i++){
        for (j = i+1; j < len; j++){    //j=i has redundant
            //temp = 0 is redundant
            if(arr[i] < arr[j]){    //less than instead of greater than
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr; //from biggest to smallest, QuickSort

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值